<!DOCTYPE html>
<html lang="en-US" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>为什么要分库分表 | VitePress</title>
    <meta name="description" content="A VitePress site">
    <link rel="preload stylesheet" href="/notebook/assets/style.3dbfd0c2.css" as="style">
    
    <script type="module" src="/notebook/assets/app.8aaa4cbe.js"></script>
    <link rel="preload" href="/notebook/assets/inter-roman-latin.2ed14f66.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/notebook/assets/chunks/framework.1336c4e5.js">
    <link rel="modulepreload" href="/notebook/assets/chunks/theme.20cddc0c.js">
    <link rel="modulepreload" href="/notebook/assets/2、数据库_MySQL_分库分表.md.e1c8a095.lean.js">
    <script id="check-dark-light">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-255ec12d><!--[--><!--]--><!--[--><span tabindex="-1" data-v-ae3e3f51></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-ae3e3f51> Skip to content </a><!--]--><!----><header class="VPNav" data-v-255ec12d data-v-7e5bc4a5><div class="VPNavBar has-sidebar" data-v-7e5bc4a5 data-v-0937f67c><div class="container" data-v-0937f67c><div class="title" data-v-0937f67c><div class="VPNavBarTitle has-sidebar" data-v-0937f67c data-v-86d1bed8><a class="title" href="/notebook/" data-v-86d1bed8><!--[--><!--]--><!--[--><img class="VPImage logo" src="/notebook/Vue.png" alt data-v-8426fc1a><!--]--><!--[-->任硕的文档<!--]--><!--[--><!--]--></a></div></div><div class="content" data-v-0937f67c><div class="curtain" data-v-0937f67c></div><div class="content-body" data-v-0937f67c><!--[--><!--]--><div class="VPNavBarSearch search" style="--vp-meta-key:&#39;Meta&#39;;" data-v-0937f67c><!--[--><!----><div id="docsearch"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg class="DocSearch-Search-Icon" width="20" height="20" viewBox="0 0 20 20" aria-label="search icon"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-0937f67c data-v-7f418b0f><span id="main-nav-aria-label" class="visually-hidden" data-v-7f418b0f>Main Navigation</span><!--[--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>Java学前端</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/HTML+JS.html" data-v-2f2cfafc><!--[-->HTML+JS<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/CSS.html" data-v-2f2cfafc><!--[-->CSS<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/Vue2+%E7%BB%84%E4%BB%B6.html" data-v-2f2cfafc><!--[-->Vue2+组件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/Vue3+%E7%BB%84%E4%BB%B6.html" data-v-2f2cfafc><!--[-->Vue3+组件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/React.html" data-v-2f2cfafc><!--[-->React<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>软件测试</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80.html" data-v-2f2cfafc><!--[-->测试基础<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E5%8E%8B%E5%8A%9B%E6%B5%8B%E8%AF%95.html" data-v-2f2cfafc><!--[-->压力测试<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>多线程</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E5%B9%B6%E5%8F%91%20&amp;%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-2f2cfafc><!--[-->基础篇<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E5%B9%B6%E5%8F%91%20&amp;%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%B9%B6%E5%8F%91%E5%AE%8C%E5%96%84.html" data-v-2f2cfafc><!--[-->进阶篇<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>开发工具</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/Chrome.html" data-v-2f2cfafc><!--[-->Chrome<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/IDEA%E5%9F%BA%E7%A1%80.html" data-v-2f2cfafc><!--[-->IDEA基础<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/IDEA%E6%8F%92%E4%BB%B6.html" data-v-2f2cfafc><!--[-->IDEA插件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/VS%20Code.html" data-v-2f2cfafc><!--[-->VS Code<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>消息中间件</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/RabbitMQ.html" data-v-2f2cfafc><!--[-->RabbitMQ<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/RocketMQ.html" data-v-2f2cfafc><!--[-->RocketMQ<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/Kafka.html" data-v-2f2cfafc><!--[-->Kafka<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/Canal.html" data-v-2f2cfafc><!--[-->Canal<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-0937f67c data-v-f6a63727><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-f6a63727 data-v-82b282f1 data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-82b282f1><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-82b282f1><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-0937f67c data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/renshuo123/renshuo123.github.io" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="#" aria-label="twitter" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg></a><a class="VPSocialLink no-icon" href="https://github.com/" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg t="1676028692954" class="icon" ...</path></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-0937f67c data-v-40855f84 data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-a7b5672a><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="icon" data-v-a7b5672a><circle cx="12" cy="12" r="2"></circle><circle cx="19" cy="12" r="2"></circle><circle cx="5" cy="12" r="2"></circle></svg></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><!----><!--[--><!--[--><!----><div class="group" data-v-40855f84><div class="item appearance" data-v-40855f84><p class="label" data-v-40855f84>Appearance</p><div class="appearance-action" data-v-40855f84><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-40855f84 data-v-82b282f1 data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-82b282f1><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-82b282f1><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div></div></div><div class="group" data-v-40855f84><div class="item social-links" data-v-40855f84><div class="VPSocialLinks social-links-list" data-v-40855f84 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/renshuo123/renshuo123.github.io" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="#" aria-label="twitter" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg></a><a class="VPSocialLink no-icon" href="https://github.com/" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg t="1676028692954" class="icon" ...</path></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-0937f67c data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><!----></header><div class="VPLocalNav reached-top" data-v-255ec12d data-v-5cfd5582><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-5cfd5582><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="menu-icon" data-v-5cfd5582><path d="M17,11H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,11,17,11z"></path><path d="M21,7H3C2.4,7,2,6.6,2,6s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,7,21,7z"></path><path d="M21,15H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,15,21,15z"></path><path d="M17,19H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,19,17,19z"></path></svg><span class="menu-text" data-v-5cfd5582>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-5cfd5582 data-v-18201f51><button data-v-18201f51>Return to top</button><!----></div></div><aside class="VPSidebar" data-v-255ec12d data-v-845b8fc6><div class="curtain" data-v-845b8fc6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-845b8fc6><span class="visually-hidden" id="sidebar-aria-label" data-v-845b8fc6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Java</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E6%96%B0%E7%89%B9%E6%80%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E9%9B%86%E5%90%88.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java集合</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java高级</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Linux</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Linux%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Linux基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Linux%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Linux新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Shell.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Shell脚本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/%E5%AE%9E%E7%94%A8%E8%84%9A%E6%9C%AC.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>实用脚本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/%E8%BD%AF%E4%BB%B6%E9%83%A8%E7%BD%B2.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>软件部署</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Nginx</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E5%AE%9E%E6%88%98%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>实战篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E9%9D%A2%E8%AF%95%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>面试篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SSM</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/Maven.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Maven</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/Spring.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Spring</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/SpringMVC.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringMVC</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/SpringBatch.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringBatch</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringBoot</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E5%BA%94%E7%94%A8%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>应用篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E6%96%B0%E7%89%B9%E6%80%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E8%BF%90%E7%BB%B4&amp;%E5%8E%9F%E7%90%86.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>运维&原理</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringCloud</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringCloud</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BF%85%E5%A4%87/Sentinel.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Sentinel</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringSecurity</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E9%AB%98%E7%BA%A7%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity高级篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Mybatis & MybatisPlus</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/Mybatis.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Mybatis</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/MybatisPlus.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MybatisPlus</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/JPA.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JPA</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Git & ChatGPT</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Git.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Git</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Github.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Github</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/ChatGPT.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ChatGPT</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Jenkins.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Jenkins</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Netty.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Netty</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible has-active" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>数据库</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible has-active" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>MySQL</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%AE%BE%E8%AE%A1.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL设计</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%90%E7%BB%B4.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL运维</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分库分表</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>Redis</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%8E%9F%E7%90%86.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis原理</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis高级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%AE%9E%E6%88%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis实战</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/%E6%9C%AC%E5%9C%B0%E7%BC%93%E5%AD%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>本地缓存</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>MongoDB</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MongoDB基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E6%95%B4%E5%90%88.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MongoDB进阶</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>ElasticSearch</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch/1%E3%80%81ES%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch/3%E3%80%81ES%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES高级</p><!--]--></a><!----></div><!----></div><!--]--></div></section><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/influxdb.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>InfluxDB</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Neo4j.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Neo4j</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>高并发 & 秒杀 & 分布式</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E5%88%86%E5%B8%83%E5%BC%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分布式理论</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BF%85%E5%A4%87/%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分布式锁</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E7%A7%92%E6%9D%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>秒杀</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E9%AB%98%E5%8F%AF%E7%94%A8.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高可用</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E9%AB%98%E5%B9%B6%E5%8F%91.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高并发</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>云原生</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%BA%91%E5%8E%9F%E7%94%9F/Docker.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Docker</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%BA%91%E5%8E%9F%E7%94%9F/K8S.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>K8S</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>可视化 & 监控</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E7%9B%91%E6%8E%A7%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>监控基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E7%9B%91%E6%8E%A7%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>监控进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%A7%E5%B1%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>可视化大屏</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/Zabbix.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Zabbix</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>学前端</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>HTML+CSS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/HTML%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>HTML基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/CSS%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>CSS基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/%E7%BD%91%E9%A1%B5%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>网页进阶</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>JS+TS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/JS%20%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JS基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/JS%20%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JS进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/ES6%20%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES6基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/ES6%20%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES6进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/TypeScript.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>TS基础</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>NodeJS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Node基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Node进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>Vue</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3高级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E6%96%B0%E8%AF%AD%E6%B3%95.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3新语法</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue2/Vue2%E9%A1%B9%E7%9B%AE.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>小程序</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>小程序基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>小程序优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/uniapp.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>uniapp</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%A1%B9%E7%9B%AE.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>计算机基础</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>数据结构</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>操作系统</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>设计模式</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/%E7%BD%91%E7%BB%9C%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>计算机网络</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/UML.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>UML</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E7%AE%97%E6%B3%95/LeetCode.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>LeetCode</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>项目实战</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>云尚办公</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E4%BA%91%E5%B0%9A%E5%8A%9E%E5%85%AC/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>小兔鲜</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E8%BF%9B%E9%98%B6%E7%AF%871.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇1</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E8%BF%9B%E9%98%B6%E7%AF%872.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇2</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>地图</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>苍穹外卖</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E8%8B%8D%E7%A9%B9%E5%A4%96%E5%8D%96/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>黑马头条</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E8%BF%9B%E9%98%B6%E7%AF%872.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇2</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E9%AB%98%E7%BA%A7%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高级篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E6%94%AF%E4%BB%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>支付</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%A1%B9%E7%9B%AE%E6%8E%A8%E8%8D%90.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目推荐</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0" data-v-845b8fc6 data-v-9b797284><!----><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/team.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>团队成员</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-255ec12d data-v-669faec9><div class="VPDoc has-sidebar has-aside" data-v-669faec9 data-v-6b87e69f><!--[--><!--]--><div class="container" data-v-6b87e69f><div class="aside" data-v-6b87e69f><div class="aside-curtain" data-v-6b87e69f></div><div class="aside-container" data-v-6b87e69f><div class="aside-content" data-v-6b87e69f><div class="VPDocAside" data-v-6b87e69f data-v-3f215769><!--[--><!--]--><!--[--><!--]--><div class="VPDocAsideOutline" data-v-3f215769 data-v-ff0f39c8><div class="content" data-v-ff0f39c8><div class="outline-marker" data-v-ff0f39c8></div><div class="outline-title" data-v-ff0f39c8>On this page</div><nav aria-labelledby="doc-outline-aria-label" data-v-ff0f39c8><span class="visually-hidden" id="doc-outline-aria-label" data-v-ff0f39c8> Table of Contents for current page </span><ul class="root" data-v-ff0f39c8 data-v-d0ee3533><!--[--><!--]--></ul></nav></div></div><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-6b87e69f><div class="content-container" data-v-6b87e69f><!--[--><!--]--><!----><main class="main" data-v-6b87e69f><div style="position:relative;" class="vp-doc _notebook_2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93_MySQL_%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8" data-v-6b87e69f><div><h1 id="为什么要分库分表" tabindex="-1">为什么要分库分表 <a class="header-anchor" href="#为什么要分库分表" aria-label="Permalink to &quot;为什么要分库分表&quot;">​</a></h1><h2 id="前言" tabindex="-1">前言 <a class="header-anchor" href="#前言" aria-label="Permalink to &quot;前言&quot;">​</a></h2><blockquote><p>分库分表是为了解决由于库、表数据量过大，而导致数据库性能持续下降的问题。按照一定的规则，将原本数据量大的数据库拆分成多个单独的数据库，将原本数据量大的表拆分成若干个数据表，使得单一的库、表性能达到最优的效果`（响应速度快），以此提升整体数据库性能。</p></blockquote><blockquote><p>分库分表的核心理念就是对数据进行切分（Sharding），以及切分后如何对数据的快速定位与查询结果整合。而分库与分表都可以从：垂直（纵向）和 水平（横向）两种纬度进行切分。</p></blockquote><h2 id="为什么要分库分表-1" tabindex="-1">为什么要分库分表 <a class="header-anchor" href="#为什么要分库分表-1" aria-label="Permalink to &quot;为什么要分库分表&quot;">​</a></h2><blockquote><p>首先要明确一个问题，单一的数据库是否能够满足公司目前的线上业务需求，比如用户表，可能有几千万，甚至上亿的数据，只是说可能，如果有这么多用户，那必然是大公司了，那么这个时候，如果不分表也不分库的话，那么数据了上来的时候，MySQL单机磁盘容量会撑爆，但是如果拆成多个数据库，磁盘使用率大大降低。</p></blockquote><blockquote><p>这样就把磁盘使用率降低，这是通过硬件的形式解决问题，如果数据量是巨大的，这时候，SQL 如果没有命中索引，那么就会导致一个情况，查这个表的SQL语句直接把数据库给干崩了。</p></blockquote><blockquote><p>即使SQL命中了索引，如果表的数据量 超过一千万的话， 查询也是会明显变慢的。这是因为索引一般是B+树结构，数据千万级别的话，B+树的高度会增高，查询自然就变慢了，当然，这是题外话了。</p></blockquote><blockquote><p>1.IO瓶颈：热点数据太多，数据库缓存不足，产生大量磁盘IO，效率较低。 请求数据太多，带宽不够，网络IO</p><p>2.CPU瓶颈：排序、分组、连接查询、聚合统计等SQL会耗费大量的CPU资源，请求数太多，CPU出现瓶颈。</p></blockquote><h2 id="_1-垂直方向" tabindex="-1">1 垂直方向 <a class="header-anchor" href="#_1-垂直方向" aria-label="Permalink to &quot;1 垂直方向&quot;">​</a></h2><p><code>垂直方向</code>主要针对的是<code>业务</code>，下面聊聊业务的发展跟分库分表有什么关系。</p><h3 id="_1-单库" tabindex="-1">1 单库 <a class="header-anchor" href="#_1-单库" aria-label="Permalink to &quot;1 单库&quot;">​</a></h3><p>在系统初期，业务功能相对来说比较简单，系统模块较少。为了快速满足迭代需求，减少一些不必要的依赖。更重要的是减少系统的复杂度，保证开发速度，我们通常会使用<code>单库</code>来保存数据。系统初期的数据库架构如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211758820.png" alt="image-20220421175848694" style="zoom:50%;"><p>此时，使用的数据库方案是：<code>一个数据库</code>包含<code>多张业务表</code>。用户读数据请求和写数据请求，都操作的同一个数据库。</p><h3 id="_2-分表" tabindex="-1">2 分表 <a class="header-anchor" href="#_2-分表" aria-label="Permalink to &quot;2 分表&quot;">​</a></h3><p>**概念：**以字段为依据，按照字段的活跃性，将表中字段拆到不同的表（主表和扩展表）中。系统上线之后，随着业务的发展，不断的添加新功能。导致单表中的字段越来越多，开始变得有点不太好维护了。一个用户表就包含了几十甚至上百个字段，管理起来有点混乱。这时候该怎么办呢？</p><p>答：分表。将<code>用户表</code>拆分为：<code>用户基本信息表</code> 和 <code>用户扩展表</code>。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211759113.png" alt="image-20220421175926995" style="zoom:67%;"><p>用户基本信息表中存的是用户最主要的信息，比如：用户名、密码、别名、手机号、邮箱、年龄、性别等核心数据。</p><p>这些信息跟用户息息相关，查询的频次非常高。</p><p>而用户扩展表中存的是用户的扩展信息，比如：所属单位、户口所在地、所在城市等等，非核心数据。</p><p>这些信息只有在特定的业务场景才需要查询，而绝大数业务场景是不需要的。</p><p>所以通过分表把核心数据和非核心数据分开，让表的结构更清晰，职责更单一，更便于维护。</p><p>除了按实际业务分表之外，我们还有一个常用的分表原则是：把调用频次高的放在一张表，调用频次低的放另一张表</p><p>有个非常经典的例子就是：订单表和订单详情表。</p><p><strong>结果：</strong></p><ul><li>每个表的结构都不一样；</li><li>每个表的数据也不一样，一般来说，每个表的字段至少有一列交集，一般是主键，用于关联数据；</li><li>所有表的并集是全量数据；</li></ul><p>**场景：**系统绝对并发量并没有上来，表的记录并不多，但是字段多，并且热点数据和非热点数据在一起，单行数据所需的存储空间较大。以至于数据库缓存的数据行减少，查询时会去读磁盘数据产生大量的随机读IO，产生IO瓶颈。</p><p>**分析：**可以用列表页和详情页来帮助理解。垂直分表的拆分原则是将热点数据（可能会冗余经常一起查询的数据）放在一起作为主表，非热点数据放在一起作为扩展表。这样更多的热点数据就能被缓存下来，进而减少了随机读IO。拆了之后，要想获得全部数据就需要关联两个表来取数据。</p><p>但记住，千万别用join，因为join不仅会增加CPU负担并且会讲两个表耦合在一起（必须在一个数据库实例上）。关联数据，应该在业务Service层做文章，分别获取主表和扩展表数据然后用关联字段关联得到全部数据。</p><h3 id="_3-分库" tabindex="-1">3 分库 <a class="header-anchor" href="#_3-分库" aria-label="Permalink to &quot;3 分库&quot;">​</a></h3><p>**概念：**以表为依据，按照业务归属不同，将不同的表拆分到不同的库中。</p><p>不知不觉，系统已经上线了一年多的时间了。经历了N个迭代的需求开发，功能已经非常完善。</p><p>系统功能完善，意味着系统各种关联关系，错综复杂。</p><p>此时，如果不赶快梳理业务逻辑，后面会带来很多隐藏问题，会把自己坑死。</p><p>这就需要按业务功能，划分不同领域了。把相同领域的表放到同一个数据库，不同领域的表，放在另外的数据库。</p><p>具体拆分过程如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211800522.png" alt="image-20220421180032389" style="zoom:67%;"><p>将用户、产品、物流、订单相关的表，从原来一个数据库中，拆分成单独的用户库、产品库、物流库和订单库，一共四个数据库。</p><blockquote><p>在这里为了看起来更直观，每个库我只画了一张表，实际场景可能有多张表。</p></blockquote><p>这样按领域拆分之后，每个领域只用关注自己相关的表，职责更单一了，一下子变得更好维护了。</p><p><strong>结果：</strong></p><ul><li>每个库的结构都不一样；</li><li>每个库的数据也不一样，没有交集；</li><li>所有库的并集是全量数据；</li></ul><p>**场景：**系统绝对并发量上来了，并且可以抽象出单独的业务模块。</p><p>**分析：**到这一步，基本上就可以服务化了。例如，随着业务的发展一些公用的配置表、字典表等越来越多，这时可以将这些表拆到单独的库中，甚至可以服务化。再有，随着业务的发展孵化出了一套业务模式，这时可以将相关的表拆到单独的库中，甚至可以服务化。</p><h3 id="_4-分库分表" tabindex="-1">4 分库分表 <a class="header-anchor" href="#_4-分库分表" aria-label="Permalink to &quot;4 分库分表&quot;">​</a></h3><p>有时候按业务，只分库，或者只分表是不够的。比如：有些财务系统，需要按月份和年份汇总，所有用户的资金。</p><p>这就需要做：<code>分库分表</code>了。</p><p>每年都有个单独的数据库，每个数据库中，都有12张表，每张表存储一个月的用户资金数据。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211802172.png" alt="image-20220421180227044" style="zoom:50%;"><p>这样分库分表之后，就能非常高效的查询出某个用户每个月，或者每年的资金了。</p><p>此外，还有些比较特殊的需求，比如需要按照地域分库，比如：华中、华北、华南等区，每个区都有一个单独的数据库。甚至有些游戏平台，按接入的游戏厂商来做分库分表。</p><h3 id="_5-垂直切分优缺点" tabindex="-1">5 垂直切分优缺点 <a class="header-anchor" href="#_5-垂直切分优缺点" aria-label="Permalink to &quot;5 垂直切分优缺点&quot;">​</a></h3><p><strong>垂直切分的优点</strong>：</p><blockquote><ul><li>业务间数据解耦，不同业务的数据进行独立的维护、监控、扩展。</li><li>在高并发场景下，一定程度上缓解了数据库的压力。</li></ul></blockquote><p><strong>垂直切分的缺点</strong>：</p><blockquote><ul><li>提升了开发的复杂度，由于业务的隔离性，很多表无法直接访问，必须通过接口方式聚合数据。</li><li>分布式事务管理难度增加。</li><li>数据库还是存在单表数据量过大的问题，并未根本上解决，需要配合水平切分。</li></ul></blockquote><h2 id="_2-水平方向" tabindex="-1">2 水平方向 <a class="header-anchor" href="#_2-水平方向" aria-label="Permalink to &quot;2 水平方向&quot;">​</a></h2><p><code>水分方向</code>主要针对的是<code>数据</code>，下面聊聊数据跟分库分表又有什么关系。</p><h3 id="_1-单库-1" tabindex="-1">1 单库 <a class="header-anchor" href="#_1-单库-1" aria-label="Permalink to &quot;1 单库&quot;">​</a></h3><p>在系统初期，由于用户非常少，所以系统并发量很小。并且存在表中的数据量也非常少。</p><p>这时的数据库架构如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211802839.png" alt="image-20220421180257701" style="zoom:67%;"><p>此时，使用的数据库方案同样是：<code>一个master数据库</code>包含<code>多张业务表</code>。</p><p>用户读数据请求和写数据请求，都是操作的同一个数据库，该方案比较适合于并发量很低的业务场景。</p><h3 id="_2-主从读写分离" tabindex="-1">2 主从读写分离 <a class="header-anchor" href="#_2-主从读写分离" aria-label="Permalink to &quot;2 主从读写分离&quot;">​</a></h3><p>系统上线一段时间后，用户数量增加了。此时，你会发现用户的请求当中，读数据的请求占据了大部分，真正写数据的请求占比很少。众所周知，<code>数据库连接是有限的</code>，它是非常宝贵的资源。而每次数据库的读或写请求，都需要占用至少一个数据库连接。</p><p>如果写数据请求需要的数据库连接，被读数据请求占用完了，不就写不了数据了？这样问题就严重了。</p><p>为了解决该问题，我们需要把<code>读库</code>和<code>写库</code>分开。于是，就出现了主从读写分离架构：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211803444.png" alt="image-20220421180338319" style="zoom:67%;"><p>考虑刚开始用户量还没那么大，选择的是<code>一主一从</code>的架构，也就是常说的一个master一个slave。</p><p>所有的写数据请求，都指向主库。一旦主库写完数据之后，立马异步同步给从库。这样所有的读数据请求，就能及时从从库中获取到数据了（除非网络有延迟）。</p><p>读写分离方案可以解决上面提到的单节点问题，相对于单库的方案，能够更好的保证系统的稳定性。</p><p>因为如果主库挂了，可以升级从库为主库，将所有读写请求都指向新主库，系统又能正常运行了。</p><blockquote><p>读写分离方案其实也是分库的一种，它相对于为数据做了备份，它已经成为了系统初期的首先方案。</p></blockquote><p>但这里有个问题就是：如果用户量确实有些大，如果master挂了，升级slave为master，将所有读写请求都指向新master。</p><p>但此时，如果这个新master根本扛不住所有的读写请求，该怎么办？</p><p>这就需要<code>一主多从</code>的架构了：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211804749.png" alt="image-20220421180433612" style="zoom:67%;"><p>上图中我列的是<code>一主两从</code>，如果master挂了，可以选择从库1或从库2中的一个，升级为新master。假如我们在这里升级从库1为新master，则原来的从库2就变成了新master的的slave了。</p><p>调整之后的架构图如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211805792.png" alt="image-20220421180515645" style="zoom:50%;"><h3 id="_3-分库-1" tabindex="-1">3 分库 <a class="header-anchor" href="#_3-分库-1" aria-label="Permalink to &quot;3 分库&quot;">​</a></h3><p>**概念：**以字段为依据，按照一定策略（hash、range等），将一个库中的数据拆分到多个库中。</p><p>上面的读写分离方案确实可以解决读请求大于写请求时，导致master节点扛不住的问题。但如果某个领域，比如：用户库。如果注册用户的请求量非常大，即写请求本身的请求量就很大，一个master库根本无法承受住这么大的压力。</p><p>这时该怎么办呢？答：建立多个用户库。用户库的拆分过程如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211805116.png" alt="image-20220421180552987" style="zoom:67%;"><p><strong>结果：</strong></p><ul><li>每个库的结构都一样；</li><li>每个库的数据都不一样，没有交集；</li><li>所有库的并集是全量数据；</li></ul><p>**场景：**系统绝对并发量上来了，分表难以根本上解决问题，并且还没有明显的业务归属来垂直分库。</p><p>**分析：**库多了，io和cpu的压力自然可以成倍缓解。</p><h3 id="_4-分表" tabindex="-1">4 分表 <a class="header-anchor" href="#_4-分表" aria-label="Permalink to &quot;4 分表&quot;">​</a></h3><p>**概念：**以字段为依据，按照一定策略（hash、range等），将一个表中的数据拆分到多个表中</p><p>用户请求量上来了，带来的势必是数据量的成本上升。即使做了分库，但有可能单个库，比如：用户库，出现了5000万的数据。</p><p>根据经验值，单表的数据量应该尽量控制在1000万以内，性能是最佳的。如果有几千万级的数据量，用单表来存，性能会变得很差。</p><p>如果数据量太大了，需要建立的索引也会很大，从小到大检索一次数据，会非常耗时，而且非常消耗cpu资源。</p><p>这时该怎么办呢？</p><p>答：<code>分表</code>，这样可以控制每张表的数据量，和索引大小。</p><p>表拆分过程如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211806143.png" alt="image-20220421180641011" style="zoom:50%;"><p>我在这里将用户库中的用户表，拆分成了四张表（真实场景不一定是这样的），每张表的表结构是一模一样的，只是存储的数据不一样。</p><p>如果以后用户数据量越来越大，只需再多分几张用户表即可。</p><p><strong>结果：</strong></p><ul><li>每个表的结构都一样；</li><li>每个表的数据都不一样，没有交集；</li><li>所有表的并集是全量数据；</li></ul><p>**场景：**系统绝对并发量并没有上来，只是单表的数据量太多，影响了SQL效率，加重了CPU负担，以至于成为瓶颈。推荐：<a href="http://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&amp;mid=2247491313&amp;idx=2&amp;sn=01d82309c459c7385a2ccf0018bb0d8a&amp;chksm=ebd621dddca1a8cb16f33497c45aeb44e95ca0d5289afee263e047a868cb67f34ada644418b0&amp;scene=21#wechat_redirect" target="_blank" rel="noreferrer">一次SQL查询优化原理分析</a></p><p>**分析：**表的数据量少了，单次SQL执行效率高，自然减轻了CPU的负担。</p><h3 id="_5-分库分表" tabindex="-1">5 分库分表 <a class="header-anchor" href="#_5-分库分表" aria-label="Permalink to &quot;5 分库分表&quot;">​</a></h3><p>当系统发展到一定的阶段，用户并发量大，而且需要存储的数据量也很多。这时该怎么办呢？</p><p>答：需要做<code>分库分表</code>。如下图所示：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211807831.png" alt="image-20220421180714692" style="zoom:67%;"><p>图中将用户库拆分成了三个库，每个库都包含了四张用户表。</p><p>如果有用户请求过来的时候，先根据用户id路由到其中一个用户库，然后再定位到某张表。</p><p>路由的算法挺多的：</p><ul><li><code>根据id取模</code>，比如：id=7，有4张表，则7%4=3，模为3，路由到用户表3。</li><li><code>给id指定一个区间范围</code>，比如：id的值是0-10万，则数据存在用户表0，id的值是10-20万，则数据存在用户表1。</li><li><code>一致性hash算法</code></li></ul><p>这篇文章就不过多介绍了，后面会有文章专门介绍这些路由算法的。</p><h2 id="_3-真实案例" tabindex="-1">3 真实案例 <a class="header-anchor" href="#_3-真实案例" aria-label="Permalink to &quot;3 真实案例&quot;">​</a></h2><p>接下来，废话不多说，给大家分享三个我参与过的分库分表项目经历，给有需要的朋友一个参考。</p><h3 id="_1-分库" tabindex="-1">1 分库 <a class="header-anchor" href="#_1-分库" aria-label="Permalink to &quot;1 分库&quot;">​</a></h3><p>我之前待过一家公司，我们团队是做游戏运营的，我们公司提供平台，游戏厂商接入我们平台，推广他们的游戏。</p><p>游戏玩家通过我们平台登录，成功之后跳转到游戏厂商的指定游戏页面，该玩家就能正常玩游戏了，还可以充值游戏币。</p><p>这就需要建立我们的账号体系和游戏厂商的账号的映射关系，游戏玩家通过登录我们平台的游戏账号，成功之后转换成游戏厂商自己平台的账号。</p><p>这里有两个问题：</p><ol><li>每个游戏厂商的接入方式可能都不一样，账号体系映射关系也有差异。</li><li>用户都从我们平台登录，成功之后跳转到游戏厂商的游戏页面。当时有N个游戏厂商接入了，活跃的游戏玩家比较多，登录接口的并发量不容小觑。</li></ol><p>为了解决这两个问题，我们当时采用的方案是：<code>分库</code>。即针对每一个游戏都单独建一个数据库，数据库中的表结构允许存在差异。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211807434.png" alt="image-20220421180745318" style="zoom:67%;"><h3 id="_2-分表-1" tabindex="-1">2 分表 <a class="header-anchor" href="#_2-分表-1" aria-label="Permalink to &quot;2 分表&quot;">​</a></h3><p>还是在那家游戏平台公司，我们还有另外一个业务就是：<code>金钻会员</code>。说白了就是打造了一套跟游戏相关的会员体系，为了保持用户的活跃度，开通会员有很多福利，比如：送游戏币、充值有折扣、积分兑换、抽奖、专属客服等等。</p><p>在这套会员体系当中，有个非常重要的功能就是：<code>积分</code>。用户有很多种途径可以获取积分，比如：签到、充值、玩游戏、抽奖、推广、参加活动等等。积分用什么用途呢？</p><ol><li>退换实物礼物</li><li>兑换游戏币</li><li>抽奖</li></ol><p>说了这么多，其实就是想说，一个用户一天当中，获取积分或消费积分都可能有很多次，那么，一个用户一天就可能会产生几十条记录。如果用户多了的话，积分相关的数据量其实挺惊人的。我们当时考虑了，水平方向的数据量可能会很大，但是用户并发量并不大，不像登录接口那样。所以采用的方案是：<code>分表</code>。</p><p>当时使用一个积分数据库就够了，但是分了128张表。然后根据用户id，进行hash除以128取模。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211808217.png" alt="image-20220421180818087" style="zoom:67%;"><blockquote><p>需要特别注意的是，分表的数量最好是2的幂次方，方便以后扩容。</p></blockquote><h3 id="_3-分库分表" tabindex="-1">3 分库分表 <a class="header-anchor" href="#_3-分库分表" aria-label="Permalink to &quot;3 分库分表&quot;">​</a></h3><p>后来我去了一家从事餐饮软件开发的公司。这个公司有个特点是在每天的中午和晚上的就餐高峰期，用户的并发量很大。用户吃饭前需要通过我们系统点餐，然后下单，然后结账。当时点餐和下单的并发量挺大的。</p><p>餐厅可能会有很多人，每个人都可能下多个订单。这样就会导致用户的并发量高，并且数据量也很大。</p><p>所以，综合考虑了一下，当时我们采用的技术方案是：<code>分库分表</code>。</p><p>经过调研之后，觉得使用了当当网开源的基于jdbc的中间件框架：<code>sharding-jdbc</code>。</p><p>当时分了4个库，每个库有32张表。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204211808597.png" alt="image-20220421180845461" style="zoom:67%;"><h2 id="_4-一定规则是什么" tabindex="-1">4 一定规则是什么 <a class="header-anchor" href="#_4-一定规则是什么" aria-label="Permalink to &quot;4 一定规则是什么&quot;">​</a></h2><p>我们上边提到过很多次 <code>一定规则</code> ，这个规则其实是一种路由算法，就是决定一条数据具体应该存在哪个数据库的哪张表里。常见的有 <code>取模算法</code> 和 <code>范围限定算法</code></p><h3 id="_1、取模算法" tabindex="-1">1、取模算法 <a class="header-anchor" href="#_1、取模算法" aria-label="Permalink to &quot;1、取模算法&quot;">​</a></h3><p>按字段取模（对hash结果取余数 (hash() mod N)，N为数据库实例数或子表数量）是最为常见的一种切分方式。</p><p>还拿 <code>order</code> 订单表举例，先对数据库从 0 到 N-1进行编号，对 <code>order</code> 订单表中 <code>work_no</code> 订单编号字段进行取模，得到余数 <code>i</code>，<code>i=0</code>存第一个库，<code>i=1</code>存第二个库，<code>i=2</code>存第三个库....以此类推。</p><p>这样同一笔订单的数据都会存在同一个库、表里，查询时用相同的规则，用 <code>work_no</code> 订单编号作为查询条件，就能快速的定位到数据。</p><p><strong>优点：</strong></p><p>数据分片相对比较均匀，不易出现请求都打到一个库上的情况。</p><p><strong>缺点：</strong></p><p>这种算法存在一些问题，当某一台机器宕机，本应该落在该数据库的请求就无法得到正确的处理，这时宕掉的实例会被踢出集群，此时算法变成hash(userId) mod N-1，用户信息可能就不再在同一个库中了。</p><h3 id="_2、范围限定算法" tabindex="-1">2、范围限定算法 <a class="header-anchor" href="#_2、范围限定算法" aria-label="Permalink to &quot;2、范围限定算法&quot;">​</a></h3><p>按照 <code>时间区间</code> 或 <code>ID区间</code> 来切分，比如：我们切分的是用户表，可以定义每个库的 <code>User</code> 表里只存10000条数据，第一个库只存 <code>userId</code> 从1 ~ 9999的数据，第二个库存 <code>userId</code> 为10000 ~ 20000，第三个库存 <code>userId</code> 为 20001~ 30000......以此类推，按时间范围也是同理。</p><p><strong>优点：</strong></p><ul><li>单表数据量是可控的</li><li>水平扩展简单只需增加节点即可，无需对其他分片的数据进行迁移</li><li>能快速定位要查询的数据在哪个库</li></ul><p><strong>缺点：</strong></p><ul><li>由于连续分片可能存在数据热点，比如按时间字段分片，可能某一段时间内订单骤增，可能会被频繁的读写，而有些分片存储的历史数据，则很少被查询。</li></ul><h2 id="_5-分库分表的难点" tabindex="-1">5 分库分表的难点 <a class="header-anchor" href="#_5-分库分表的难点" aria-label="Permalink to &quot;5 分库分表的难点&quot;">​</a></h2><h3 id="_1、分布式事务" tabindex="-1">1、分布式事务 <a class="header-anchor" href="#_1、分布式事务" aria-label="Permalink to &quot;1、分布式事务&quot;">​</a></h3><p>由于表分布在不同库中，不可避免会带来跨库事务问题。一般可使用 &quot;<code>三阶段提交</code> &quot;和 &quot;<code>两阶段提交</code>&quot; 处理，但是这种方式性能较差，代码开发量也比较大。通常做法是做到最终一致性的方案，如果不苛求系统的实时一致性，只要在允许的时间段内达到最终一致性即可，采用事务补偿的方式。</p><p>这里我应用阿里的分布式事务框架<code>Seata</code> 来做分布式事务的管理，后边会结合实际案例。</p><h3 id="_2、分页、排序、跨库联合查询" tabindex="-1">2、分页、排序、跨库联合查询 <a class="header-anchor" href="#_2、分页、排序、跨库联合查询" aria-label="Permalink to &quot;2、分页、排序、跨库联合查询&quot;">​</a></h3><p>分页、排序、联合查询是开发中使用频率非常高的功能，但在分库分表后，这些看似普通的操作却是让人非常头疼的问题。将分散在不同库中表的数据查询出来，再将所有结果进行汇总整理后提供给用户。</p><h3 id="_3、分布式主键" tabindex="-1">3、分布式主键 <a class="header-anchor" href="#_3、分布式主键" aria-label="Permalink to &quot;3、分布式主键&quot;">​</a></h3><p>分库分表后数据库的自增主键意义就不大了，因为我们不能依靠单个数据库实例上的自增主键来实现不同数据库之间的全局唯一主键，此时一个能够生成全局唯一ID的系统是非常必要的，那么这个全局唯一ID就叫 <code>分布式ID</code>。</p><h3 id="_4、读写分离" tabindex="-1">4、读写分离 <a class="header-anchor" href="#_4、读写分离" aria-label="Permalink to &quot;4、读写分离&quot;">​</a></h3><p>不难发现大部分主流的关系型数据库都提供了主从架构的高可用方案，而我们需要实现 <code>读写分离</code> + <code>分库分表</code>，读库与写库都要做分库分表处理，后边会有具体实战案例。</p><h3 id="_5、数据脱敏" tabindex="-1">5、数据脱敏 <a class="header-anchor" href="#_5、数据脱敏" aria-label="Permalink to &quot;5、数据脱敏&quot;">​</a></h3><p>数据脱敏，是指对某些敏感信息通过脱敏规则进行数据转换，从而实现敏感隐私数据的可靠保护，如身份证号、手机号、卡号、账号密码等个人信息，一般这些都需要进行做脱敏处理。</p><h2 id="_6-分库分表工具" tabindex="-1">6 分库分表工具 <a class="header-anchor" href="#_6-分库分表工具" aria-label="Permalink to &quot;6 分库分表工具&quot;">​</a></h2><p>我还是那句话，尽量不要自己造轮子，因为自己造的轮子可能不那么圆，业界已经有了很多比较成熟的分库分表中间件，我们根据自身的业务需求挑选，将更多的精力放在业务实现上。</p><ul><li><code>sharding-jdbc</code>（当当）</li><li><code>TSharding</code>（蘑菇街）</li><li><code>Atlas</code>（奇虎360）</li><li><code>Cobar</code>（阿里巴巴）</li><li><code>MyCAT</code>（基于Cobar）</li><li><code>Oceanus</code>（58同城）</li><li><code>Vitess</code>（谷歌）</li></ul><h2 id="_7-为什么选-sharding-jdbc" tabindex="-1">7 为什么选 sharding-jdbc <a class="header-anchor" href="#_7-为什么选-sharding-jdbc" aria-label="Permalink to &quot;7 为什么选 sharding-jdbc&quot;">​</a></h2><p>sharding-jdbc是一款轻量级 Java框架，以 jar包形式提供服务，是属于客户端产品不需要额外部署，它相当于是个增强版的 <code>JDBC</code> 驱动；相比之下像 <code>Mycat</code> 这类需要单独的部署服务的服务端产品，就稍显复杂了。况且我想把更多精力放在实现业务，不想做额外的运维工作。</p><ul><li><code>sharding-jdbc</code>的兼容性也非常强大，适用于任何基于 <code>JDBC</code> 的 <code>ORM</code> 框架，如：<code>JPA</code>， <code>Hibernate</code>，<code>Mybatis</code>，<code>Spring JDBC Template</code> 或直接使用的 <code>JDBC</code>。</li><li>完美兼容任何第三方的数据库连接池，如：<code>DBCP</code>， <code>C3P0</code>， <code>BoneCP</code>，<code>Druid</code>， <code>HikariCP</code> 等，几乎对所有关系型数据库都支持。</li></ul><p>不难发现确实是比较强大的一款工具，而且它对项目的侵入性很小，几乎不用做任何代码层的修改，也无需修改 <code>SQL</code> 语句，只需配置待分库分表的数据表即可。</p><h2 id="_8-总结" tabindex="-1">8 总结 <a class="header-anchor" href="#_8-总结" aria-label="Permalink to &quot;8 总结&quot;">​</a></h2><p>上面主要从：垂直和水平，两个方向介绍了我们的系统为什么要分库分表。</p><p>说实话垂直方向（即业务方向）更简单。</p><p>在水平方向（即数据方向）上，<code>分库</code>和<code>分表</code>的作用，其实是有区别的，不能混为一谈。</p><ul><li><code>分库</code>：是为了解决数据库连接资源不足问题，和磁盘IO的性能瓶颈问题。</li><li><code>分表</code>：是为了解决单表数据量太大，sql语句查询数据时，即使走了索引也非常耗时问题。此外还可以解决消耗cpu资源问题。</li><li><code>分库分表</code>：可以解决 数据库连接资源不足、磁盘IO的性能瓶颈、检索数据耗时 和 消耗cpu资源等问题。</li></ul><p>如果在有些业务场景中，用户并发量很大，但是需要保存的数据量很少，这时可以只分库，不分表。</p><p>如果在有些业务场景中，用户并发量不大，但是需要保存的数量很多，这时可以只分表，不分库。</p><p>如果在有些业务场景中，用户并发量大，并且需要保存的数量也很多时，可以分库分表。</p><h1 id="分库分表原理" tabindex="-1">分库分表原理 <a class="header-anchor" href="#分库分表原理" aria-label="Permalink to &quot;分库分表原理&quot;">​</a></h1><p><a href="https://mp.weixin.qq.com/s?__biz=Mzg4MjU0OTM1OA==&amp;mid=2247505881&amp;idx=1&amp;sn=eb411129decbe83d1c0c00853206235c&amp;chksm=cf5660d8f821e9ce5ddbd59704fb9f3da1ddd3dc0c1f6b5bbcf8988a3ad15e4f17e0265bd44f&amp;mpshare=1&amp;scene=23&amp;srcid=10214LTIkcjmunw4ejFsj5Yo&amp;sharer_sharetime=1666366564298&amp;sharer_shareid=29b8a04db1dbd975e3bf4e9f47e7ac67#rd" target="_blank" rel="noreferrer">SpringBoot + Sharding JDBC，一文搞定分库分表、读写分离 (qq.com)</a></p><h2 id="mysql分库分表原理" tabindex="-1">MySQL分库分表原理 <a class="header-anchor" href="#mysql分库分表原理" aria-label="Permalink to &quot;MySQL分库分表原理&quot;">​</a></h2><h3 id="分库分表" tabindex="-1">分库分表 <a class="header-anchor" href="#分库分表" aria-label="Permalink to &quot;分库分表&quot;">​</a></h3><p><code>水平拆分：</code>同一个表的数据拆到不同的库不同的表中。可以根据时间、地区或某个业务键维度，也可以通过hash进行拆分，最后通过路由访问到具体的数据。拆分后的每个表结构保持一致</p><p><code>垂直拆分：</code>就是把一个有很多字段的表给拆分成多个表，或者是多个库上去。每个库表的结构都不一样，每个库表都包含部分字段。一般来说，可以根据业务维度进行拆分，如订单表可以拆分为订单、订单支持、订单地址、订单商品、订单扩展等表；也可以，根据数据冷热程度拆分，20%的热点字段拆到一个表，80%的冷字段拆到另外一个表</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210251621882.png" alt="image-20221025162115810" style="zoom:67%;"><h3 id="不停机分库分表数据迁移" tabindex="-1">不停机分库分表数据迁移 <a class="header-anchor" href="#不停机分库分表数据迁移" aria-label="Permalink to &quot;不停机分库分表数据迁移&quot;">​</a></h3><p>一般数据库的拆分也是有一个过程的，一开始是单表，后面慢慢拆成多表。那么就看下如何平滑的从MySQL单表过度到MySQL的分库分表架构</p><ul><li>利用MySQL+Canal做增量数据同步，利用分库分表中间件，将数据路由到对应的新表中</li><li>利用分库分表中间件，全量数据导入到对应的新表中</li><li>通过单表数据和分库分表数据两两比较，更新不匹配的数据到新表中</li><li>数据稳定后，将单表的配置切换到分库分表配置上</li></ul><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210251622926.png" alt="image-20221025162217816" style="zoom:67%;"><h2 id="分库分表方案" tabindex="-1">分库分表方案 <a class="header-anchor" href="#分库分表方案" aria-label="Permalink to &quot;分库分表方案&quot;">​</a></h2><p>分库分表方案，不外乎就两种，一种是垂直切分，一种是水平切分。</p><p>但是总有做开发的小伙伴不知道这垂直切分和水平切分到底是什么样的，为什么垂直切分，为什么水平切分，什么时候应该选择垂直切分，什么时候应该选择水平切分。</p><p>有人是这么说的，垂直切分是根据业务来拆分数据库，同一类业务的数据表拆分到一个独立的数据库，另一类的数据表拆分到其他数据库。</p><p>有些人不理解这个，实际上垂直切分也是有划分的，上面描述的是垂直切分数据库，可能容易让很多人不太理解，但是如果是垂直切分表，那么肯定百分之90的人都能理解。</p><h3 id="垂直切分" tabindex="-1">垂直切分 <a class="header-anchor" href="#垂直切分" aria-label="Permalink to &quot;垂直切分&quot;">​</a></h3><p>有一张Order表，表中有诸多记录，比如设计这么一张简单的表。</p><table><thead><tr><th style="text-align:left;"><strong>id</strong></th><th style="text-align:left;"><strong>order_id</strong></th><th style="text-align:left;"><strong>order_date</strong></th><th style="text-align:left;"><strong>order_type</strong></th><th style="text-align:left;"><strong>order_state</strong></th></tr></thead><tbody><tr><td style="text-align:left;">1</td><td style="text-align:left;">cd96cff0356e483caae6b2ff4e878fd6</td><td style="text-align:left;">2022-06-18 13:57:11</td><td style="text-align:left;">支付宝</td><td style="text-align:left;">1</td></tr><tr><td style="text-align:left;">2</td><td style="text-align:left;">e2496f9e22ce4391806b18480440526a</td><td style="text-align:left;">2022-06-18 14:22:33</td><td style="text-align:left;">微信</td><td style="text-align:left;">2</td></tr><tr><td style="text-align:left;">3</td><td style="text-align:left;">9e7ab5a1915c4570a9eaaaa3c01f79c1</td><td style="text-align:left;">2022-06-18 15:21:44</td><td style="text-align:left;">现金</td><td style="text-align:left;">2</td></tr></tbody></table><p>以上是简化版Order表，如果想要垂直切分，那么应该怎么处理？</p><p>直接拆分成2个表，这时候就直接就一分为2 ，咔的一下拆分成两个表</p><p>Order1</p><table><thead><tr><th style="text-align:left;"><strong>id</strong></th><th style="text-align:left;"><strong>order_id</strong></th><th style="text-align:left;"><strong>order_date</strong></th></tr></thead><tbody><tr><td style="text-align:left;">1</td><td style="text-align:left;">cd96cff0356e483caae6b2ff4e878fd6</td><td style="text-align:left;">2022-06-18 13:57:11</td></tr><tr><td style="text-align:left;">2</td><td style="text-align:left;">e2496f9e22ce4391806b18480440526a</td><td style="text-align:left;">2022-06-18 14:22:33</td></tr><tr><td style="text-align:left;">3</td><td style="text-align:left;">9e7ab5a1915c4570a9eaaaa3c01f79c1</td><td style="text-align:left;">2022-06-18 15:21:44</td></tr></tbody></table><p>Order2</p><table><thead><tr><th style="text-align:left;"><strong>id</strong></th><th style="text-align:left;"><strong>order_type</strong></th><th style="text-align:left;"><strong>order_state</strong></th></tr></thead><tbody><tr><td style="text-align:left;">1</td><td style="text-align:left;">支付宝</td><td style="text-align:left;">1</td></tr><tr><td style="text-align:left;">2</td><td style="text-align:left;">微信</td><td style="text-align:left;">2</td></tr><tr><td style="text-align:left;">3</td><td style="text-align:left;">现金</td><td style="text-align:left;">2</td></tr></tbody></table><p>这时候主键ID保持的时一致的，而这个操作，就是垂直拆分，分表的操作。</p><p>既然说了垂直拆分，那么必然就有水平拆分，</p><p>什么是水平拆分呢？实际上水平拆分的话，那真的是只有一句话。</p><h3 id="水平切分" tabindex="-1">水平切分 <a class="header-anchor" href="#水平切分" aria-label="Permalink to &quot;水平切分&quot;">​</a></h3><h4 id="按照数据来拆分" tabindex="-1">按照数据来拆分 <a class="header-anchor" href="#按照数据来拆分" aria-label="Permalink to &quot;按照数据来拆分&quot;">​</a></h4><p><code>水平拆分数据库：</code>将一张表的数据 ( 按照数据行) 分到多个不同的数据库。每个库的表结构相同，每个库都只有这张表的部分数据，当单表的数据量过大，如果继续使用水平分库，那么数据库的实例 就会不断增加，不利于系统的运维。这时候就要采用水平分表。</p><p><code>水平拆分表：</code>将一张表的数据 ( 按照数据行) ，分配到同一个数据库的多张表中，每个表都只有一部分数据。</p><p>来看看Order表进行水平拆分的话，是什么样子的。</p><p>Order1</p><table><thead><tr><th style="text-align:left;"><strong>id</strong></th><th style="text-align:left;"><strong>order_id</strong></th><th style="text-align:left;"><strong>order_date</strong></th><th style="text-align:left;"><strong>order_type</strong></th><th style="text-align:left;"><strong>order_state</strong></th></tr></thead><tbody><tr><td style="text-align:left;">1</td><td style="text-align:left;">cd96cff0356e483caae6b2ff4e878fd6</td><td style="text-align:left;">2022-06-18 13:57:11</td><td style="text-align:left;">支付宝</td><td style="text-align:left;">1</td></tr><tr><td style="text-align:left;">2</td><td style="text-align:left;">e2496f9e22ce4391806b18480440526a</td><td style="text-align:left;">2022-06-18 14:22:33</td><td style="text-align:left;">微信</td><td style="text-align:left;">2</td></tr></tbody></table><p>Order2</p><table><thead><tr><th style="text-align:left;"><strong>id</strong></th><th style="text-align:left;"><strong>order_id</strong></th><th style="text-align:left;"><strong>order_date</strong></th><th style="text-align:left;"><strong>order_type</strong></th><th style="text-align:left;"><strong>order_state</strong></th></tr></thead><tbody><tr><td style="text-align:left;">3</td><td style="text-align:left;">9e7ab5a1915c4570a9eaaaa3c01f79c1</td><td style="text-align:left;">2022-06-18 15:21:44</td><td style="text-align:left;">现金</td><td style="text-align:left;">2</td></tr></tbody></table><p>实际上就是水平的把表数据给分成了2份，这么看起来是不是就很好理解了。</p><h2 id="分库分表带来的问题" tabindex="-1">分库分表带来的问题 <a class="header-anchor" href="#分库分表带来的问题" aria-label="Permalink to &quot;分库分表带来的问题&quot;">​</a></h2><h3 id="事务问题" tabindex="-1">事务问题 <a class="header-anchor" href="#事务问题" aria-label="Permalink to &quot;事务问题&quot;">​</a></h3><p>首先，分库分表最大的隐患就是，事务的一致性， 当需要更新的内容同时分布在不同的库时，不可避免的会产生跨库的事务问题。</p><p>原来在一个数据库操 作，本地事务就可以进行控制，分库之后 一个请求可能要访问多个数据库，如何保证事务的一致性，目前还没有简单的解决方案。</p><h3 id="无法联表的问题" tabindex="-1">无法联表的问题 <a class="header-anchor" href="#无法联表的问题" aria-label="Permalink to &quot;无法联表的问题&quot;">​</a></h3><p>还有一个就是，没有办法进行联表查询了，因为，原来在一个库中的一些表，被分散到多个库，并且这些数据库可能还不在一台服务器，无法关联查询，所以相对应的业务代码可能就比较多了。</p><h3 id="分页问题" tabindex="-1">分页问题 <a class="header-anchor" href="#分页问题" aria-label="Permalink to &quot;分页问题&quot;">​</a></h3><p>分库并行查询时，如果用到了分页，每个库返回的结果集本身是无序的，只有将多个库中的数据先查出来，然后再根据排序字段在内存中进行排序，如果查询结果过大也是十分消耗资源的。</p><h3 id="分库分表的技术" tabindex="-1">分库分表的技术 <a class="header-anchor" href="#分库分表的技术" aria-label="Permalink to &quot;分库分表的技术&quot;">​</a></h3><p>目前比较流行的就两种，一种是MyCat，另外一种则是Sharding-Jdbc，都是可以进行分库的，</p><p>MyCat是一个数据库中间件，Sharding-Jdbc是以 jar 包提供服务的jdbc框架。</p><p>Mycat和Sharding-jdbc 实现原理也是不同：</p><p>Mycat的原理中最重要的一个动词是“拦截”，它拦截了用户发送过来的SQL语句，首先对SQL语句做了一些特定的分析：如分库分表分析、路由分析、读写分离分析、缓存分析等，然后将此SQL发往后端的真实数据库，并将返回的结果做适当的处理，最终再返回给用户。</p><p>而Sharding-JDBC的原理是接受到一条SQL语句时，会陆续执行SQL解析 =&gt; 查询优化 =&gt; SQL路由 =&gt; SQL改写 =&gt; SQL执行 =&gt; 结果归并 ，最终返回执行结果。</p><h3 id="小结" tabindex="-1">小结 <a class="header-anchor" href="#小结" aria-label="Permalink to &quot;小结&quot;">​</a></h3><p><strong>垂直分表</strong>：将一张宽表(字段很多的表)，按照字段的访问频次进行拆分，就是按照表单结构进行 拆。</p><p><strong>垂直分库</strong>：根据不同的业务，将表进行分类，拆分到不同的数据库。这些库可以部署在不同的服 务器，分摊访问压力。</p><p><strong>水平分库</strong>：将一张表的数据 ( 按照数据行) 分到多个不同的数据库。每个库的表结构相同</p><p><strong>水平分表</strong>：将一张表的数据 ( 按照数据行) ，分配到同一个数据库的多张表中，每个表都只有一部 分数据。</p><h1 id="shardingsphere" tabindex="-1">ShardingSphere <a class="header-anchor" href="#shardingsphere" aria-label="Permalink to &quot;ShardingSphere&quot;">​</a></h1><p><a href="https://mp.weixin.qq.com/s?__biz=Mzg4MjU0OTM1OA==&amp;mid=2247505881&amp;idx=1&amp;sn=eb411129decbe83d1c0c00853206235c&amp;chksm=cf5660d8f821e9ce5ddbd59704fb9f3da1ddd3dc0c1f6b5bbcf8988a3ad15e4f17e0265bd44f&amp;mpshare=1&amp;scene=23&amp;srcid=10214LTIkcjmunw4ejFsj5Yo&amp;sharer_sharetime=1666366564298&amp;sharer_shareid=29b8a04db1dbd975e3bf4e9f47e7ac67#rd" target="_blank" rel="noreferrer">SpringBoot + Sharding JDBC，一文搞定分库分表、读写分离 (qq.com)</a></p><h2 id="_1、简介" tabindex="-1">1、简介 <a class="header-anchor" href="#_1、简介" aria-label="Permalink to &quot;1、简介&quot;">​</a></h2><p>官网：<a href="https://shardingsphere.apache.org/index_zh.html" target="_blank" rel="noreferrer">https://shardingsphere.apache.org/index_zh.html</a></p><p>文档：<a href="https://shardingsphere.apache.org/document/5.1.1/cn/overview/" target="_blank" rel="noreferrer">https://shardingsphere.apache.org/document/5.1.1/cn/overview/</a></p><blockquote><p>Apache ShardingSphere 是一套开源的分布式数据库解决方案组成的生态圈，它由 JDBC、Proxy 和 Sidecar（规划中）这 3 款既能够独立部署，又支持混合部署配合使用的产品组成。 它们均提供标准化的数据水平扩展、分布式事务和分布式治理等功能，可适用于如 Java 同构、异构语言、云原生等各种多样化的应用场景。</p></blockquote><blockquote><p>Apache ShardingSphere 旨在充分合理地在分布式的场景下利用关系型数据库的计算和存储能力，而并非实现一个全新的关系型数据库。 关系型数据库当今依然占有巨大市场份额，是企业核心系统的基石，未来也难于撼动，我们更加注重在原有基础上提供增量，而非颠覆。</p></blockquote><blockquote><p>Apache ShardingSphere 5.x 版本开始致力于可插拔架构，项目的功能组件能够灵活的以可插拔的方式进行扩展。 目前，数据分片、读写分离、数据加密、影子库压测等功能，以及 MySQL、PostgreSQL、SQLServer、Oracle 等 SQL 与协议的支持，均通过插件的方式织入项目。 开发者能够像使用积木一样定制属于自己的独特系统。Apache ShardingSphere 目前已提供数十个 SPI 作为系统的扩展点，仍在不断增加中。ShardingSphere 已于2020年4月16日成为 Apache 软件基金会的顶级项目。</p></blockquote><h2 id="_2、shardingsphere-jdbc" tabindex="-1">2、ShardingSphere-JDBC <a class="header-anchor" href="#_2、shardingsphere-jdbc" aria-label="Permalink to &quot;2、ShardingSphere-JDBC&quot;">​</a></h2><p><strong>程序代码封装</strong></p><p>定位为轻量级 Java 框架，<code>在 Java 的 JDBC 层提供的额外服务</code>。 它使用客户端直连数据库，<code>以 jar 包形式提供服务</code>，无需额外部署和依赖，可理解为增强版的 JDBC 驱动，完全兼容 JDBC 和各种 ORM 框架。</p><p>适用于任何基于 JDBC 的 ORM 框架，如：JPA, Hibernate, Mybatis, Spring JDBC Template 或直接使用 JDBC。 支持任何第三方的数据库连接池，如：DBCP, C3P0, BoneCP, Druid, HikariCP 等。 支持任意实现 JDBC 规范的数据库，目前支持 MySQL，Oracle，SQLServer，PostgreSQL 以及任何遵循 SQL92 标准的数据库。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171530038.png" alt="image-20220804195402870" style="zoom:80%;"><h2 id="_3、shardingsphere-proxy⭐" tabindex="-1">3、ShardingSphere-Proxy⭐ <a class="header-anchor" href="#_3、shardingsphere-proxy⭐" aria-label="Permalink to &quot;3、ShardingSphere-Proxy⭐&quot;">​</a></h2><p><strong>中间件封装</strong></p><p>定位为透明化的<code>数据库代理端</code>，提供封装了数据库二进制协议的服务端版本，用于完成对异构语言的支持。 目前提供 MySQL 和 PostgreSQL版本，它可以使用任何兼容 MySQL/PostgreSQL 协议的访问客户端（如：MySQL Command Client, MySQL Workbench, Navicat 等）操作数据，对 DBA 更加友好。</p><p>向应用程序完全透明，可直接当做 MySQL/PostgreSQL 使用。</p><p>适用于任何兼容 MySQL/PostgreSQL 协议的的客户端。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171531841.png" alt="image-20220917153125771" style="zoom:80%;"><h2 id="_4、核心概念⭐" tabindex="-1">4、核心概念⭐ <a class="header-anchor" href="#_4、核心概念⭐" aria-label="Permalink to &quot;4、核心概念⭐&quot;">​</a></h2><p>在开始 <code>Sharding-JDBC</code>分库分表具体实战之前，我们有必要先了解分库分表的一些核心概念。</p><h3 id="_1、分库分表" tabindex="-1">1、分库分表 <a class="header-anchor" href="#_1、分库分表" aria-label="Permalink to &quot;1、分库分表&quot;">​</a></h3><blockquote><p>分库，显而易见，就是一个数据库分成多个数据库，部署到不同机器。</p><p>分表，就是一个数据库表分成多个表。</p></blockquote><h3 id="_2、分片" tabindex="-1">2、分片 <a class="header-anchor" href="#_2、分片" aria-label="Permalink to &quot;2、分片&quot;">​</a></h3><p>一般我们在提到分库分表的时候，大多是以水平切分模式（水平分库、分表）为基础来说的，数据分片将原本一张数据量较大的表 <code>t_order</code> 拆分生成数个表结构完全一致的小数据量表 <code>t_order_0</code>、<code>t_order_1</code>、···、<code>t_order_n</code>，每张表只存储原大表中的一部分数据，当执行一条<code>SQL</code>时会通过 <code>分库策略</code>、<code>分片策略</code> 将数据分散到不同的数据库、表内。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206051559869.png" alt="image-20220605155934811" style="zoom:67%;"><h3 id="_3、数据节点" tabindex="-1">3、数据节点 <a class="header-anchor" href="#_3、数据节点" aria-label="Permalink to &quot;3、数据节点&quot;">​</a></h3><p>数据节点是分库分表中一个不可再分的最小数据单元（表），它由数据源名称和数据表组成，例如上图中 <code>order_db_1.t_order_0</code>、<code>order_db_2.t_order_1</code> 就表示一个数据节点。</p><h3 id="_4、逻辑表" tabindex="-1">4、逻辑表 <a class="header-anchor" href="#_4、逻辑表" aria-label="Permalink to &quot;4、逻辑表&quot;">​</a></h3><p>逻辑表是指一组具有相同逻辑和数据结构表的总称。比如我们将订单表<code>t_order</code> 拆分成 <code>t_order_0</code> ··· <code>t_order_9</code> 等 10张表。此时我们会发现分库分表以后数据库中已不在有 <code>t_order</code> 这张表，取而代之的是 <code>t_order_n</code>，但我们在代码中写 <code>SQL</code> 依然按 <code>t_order</code> 来写。此时 <code>t_order</code> 就是这些拆分表的<code>逻辑表</code>。</p><h3 id="_5、真实表" tabindex="-1">5、真实表 <a class="header-anchor" href="#_5、真实表" aria-label="Permalink to &quot;5、真实表&quot;">​</a></h3><p>真实表也就是上边提到的 <code>t_order_n</code> 数据库中真实存在的物理表。</p><h3 id="_6、分片键" tabindex="-1">6、分片键 <a class="header-anchor" href="#_6、分片键" aria-label="Permalink to &quot;6、分片键&quot;">​</a></h3><p>用于分片的数据库字段。我们将 <code>t_order</code> 表分片以后，当执行一条SQL时，通过对字段 <code>order_id</code> 取模的方式来决定，这条数据该在哪个数据库中的哪个表中执行，此时 <code>order_id</code> 字段就是 <code>t_order</code> 表的分片健。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206051600597.png" alt="image-20220605160002550" style="zoom:67%;"><p>这样以来同一个订单的相关数据就会存在同一个数据库表中，大幅提升数据检索的性能，不仅如此 <code>sharding-jdbc</code> 还支持根据多个字段作为分片健进行分片。</p><h2 id="_5、分片算法" tabindex="-1">5、分片算法 <a class="header-anchor" href="#_5、分片算法" aria-label="Permalink to &quot;5、分片算法&quot;">​</a></h2><p>上边我们提到可以用分片健取模的规则分片，但这只是比较简单的一种，在实际开发中我们还希望用 <code>&gt;=</code>、<code>&lt;=</code>、<code>&gt;</code>、<code>&lt;</code>、<code>BETWEEN</code> 和 <code>IN</code> 等条件作为分片规则，自定义分片逻辑，这时就需要用到分片策略与分片算法。</p><p>从执行 SQL 的角度来看，分库分表可以看作是一种路由机制，把 SQL 语句路由到我们期望的数据库或数据表中并获取数据，分片算法可以理解成一种路由规则。</p><p>咱们先捋一下它们之间的关系，分片策略只是抽象出的概念，它是由分片算法和分片健组合而成，分片算法做具体的数据分片逻辑。</p><blockquote><p>分库、分表的分片策略配置是相对独立的，可以各自使用不同的策略与算法，每种策略中可以是多个分片算法的组合，每个分片算法可以对多个分片健做逻辑判断。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206051600013.png" alt="image-20220605160018974" style="zoom:80%;"><blockquote><p><strong>注意</strong>：sharding-jdbc 并没有直接提供分片算法的实现，需要开发者根据业务自行实现。</p></blockquote><p><code>sharding-jdbc</code> 提供了4种分片算法：</p><h3 id="_1、精确分片算法" tabindex="-1">1、精确分片算法 <a class="header-anchor" href="#_1、精确分片算法" aria-label="Permalink to &quot;1、精确分片算法&quot;">​</a></h3><p>精确分片算法（PreciseShardingAlgorithm）用于单个字段作为分片键，SQL中有 <code>=</code> 与 <code>IN</code> 等条件的分片，需要在标准分片策略（<code>StandardShardingStrategy</code> ）下使用。</p><h3 id="_2、范围分片算法" tabindex="-1">2、范围分片算法 <a class="header-anchor" href="#_2、范围分片算法" aria-label="Permalink to &quot;2、范围分片算法&quot;">​</a></h3><p>范围分片算法（RangeShardingAlgorithm）用于单个字段作为分片键，SQL中有 <code>BETWEEN AND</code>、<code>&gt;</code>、<code>&lt;</code>、<code>&gt;=</code>、<code>&lt;=</code> 等条件的分片，需要在标准分片策略（<code>StandardShardingStrategy</code> ）下使用。</p><h3 id="_3、复合分片算法" tabindex="-1">3、复合分片算法 <a class="header-anchor" href="#_3、复合分片算法" aria-label="Permalink to &quot;3、复合分片算法&quot;">​</a></h3><p>复合分片算法（ComplexKeysShardingAlgorithm）用于多个字段作为分片键的分片操作，同时获取到多个分片健的值，根据多个字段处理业务逻辑。需要在复合分片策略（<code>ComplexShardingStrategy</code> ）下使用。</p><h3 id="_4、hint分片算法" tabindex="-1">4、Hint分片算法 <a class="header-anchor" href="#_4、hint分片算法" aria-label="Permalink to &quot;4、Hint分片算法&quot;">​</a></h3><p>Hint分片算法（HintShardingAlgorithm）稍有不同，上边的算法中我们都是解析<code>SQL</code> 语句提取分片键，并设置分片策略进行分片。但有些时候我们并没有使用任何的分片键和分片策略，可还想将 SQL 路由到目标数据库和表，就需要通过手动干预指定SQL的目标数据库和表信息，这也叫强制路由。</p><h2 id="_6、分片策略" tabindex="-1">6、分片策略 <a class="header-anchor" href="#_6、分片策略" aria-label="Permalink to &quot;6、分片策略&quot;">​</a></h2><p>上边讲分片算法的时候已经说过，分片策略是一种抽象的概念，实际分片操作的是由分片算法和分片健来完成的。</p><h3 id="_1、标准分片策略" tabindex="-1">1、标准分片策略 <a class="header-anchor" href="#_1、标准分片策略" aria-label="Permalink to &quot;1、标准分片策略&quot;">​</a></h3><p>标准分片策略适用于单分片键，此策略支持 <code>PreciseShardingAlgorithm</code> 和 <code>RangeShardingAlgorithm</code> 两个分片算法。</p><p>其中 <code>PreciseShardingAlgorithm</code> 是必选的，用于处理 <code>=</code> 和 <code>IN</code> 的分片。<code>RangeShardingAlgorithm</code> 是可选的，用于处理<code>BETWEEN AND</code>， <code>&gt;</code>， <code>&lt;</code>，<code>&gt;=</code>，<code>&lt;=</code> 条件分片，如果不配置<code>RangeShardingAlgorithm</code>，SQL中的条件等将按照全库路由处理。</p><h3 id="_2、复合分片策略" tabindex="-1">2、复合分片策略 <a class="header-anchor" href="#_2、复合分片策略" aria-label="Permalink to &quot;2、复合分片策略&quot;">​</a></h3><p>复合分片策略，同样支持对 SQL语句中的 <code>=</code>，<code>&gt;</code>， <code>&lt;</code>， <code>&gt;=</code>， <code>&lt;=</code>，<code>IN</code>和 <code>BETWEEN AND</code> 的分片操作。不同的是它支持多分片键，具体分配片细节完全由应用开发者实现。</p><h3 id="_3、行表达式分片策略" tabindex="-1">3、行表达式分片策略 <a class="header-anchor" href="#_3、行表达式分片策略" aria-label="Permalink to &quot;3、行表达式分片策略&quot;">​</a></h3><p>行表达式分片策略，支持对 SQL语句中的 <code>=</code> 和 <code>IN</code> 的分片操作，但只支持单分片键。这种策略通常用于简单的分片，不需要自定义分片算法，可以直接在配置文件中接着写规则。</p><p><code>t_order_$-&gt;{t_order_id % 4}</code> 代表 <code>t_order</code> 对其字段 <code>t_order_id</code>取模，拆分成4张表，而表名分别是<code>t_order_0</code> 到 <code>t_order_3</code>。</p><h3 id="_4、hint分片策略" tabindex="-1">4、Hint分片策略 <a class="header-anchor" href="#_4、hint分片策略" aria-label="Permalink to &quot;4、Hint分片策略&quot;">​</a></h3><p>Hint分片策略，对应上边的Hint分片算法，通过指定分片健而非从 <code>SQL</code>中提取分片健的方式进行分片的策略。</p><h2 id="_7、分布式主键" tabindex="-1">7、分布式主键 <a class="header-anchor" href="#_7、分布式主键" aria-label="Permalink to &quot;7、分布式主键&quot;">​</a></h2><p>数据分⽚后，不同数据节点⽣成全局唯⼀主键是⾮常棘⼿的问题，同⼀个逻辑表（<code>t_order</code>）内的不同真实表（<code>t_order_n</code>）之间的⾃增键由于⽆法互相感知而产⽣重复主键。</p><p>尽管可通过设置⾃增主键 <code>初始值</code> 和 <code>步⻓</code> 的⽅式避免ID碰撞，但这样会使维护成本加大，乏完整性和可扩展性。如果后去需要增加分片表的数量，要逐一修改分片表的步长，运维成本非常高，所以不建议这种方式。</p><p>实现分布式主键⽣成器的方式很多，可以参考我之前写的《<a href="https://mp.weixin.qq.com/s?__biz=MjM5NTY1MjY0MQ==&amp;mid=2650762665&amp;idx=5&amp;sn=783b170510733f03d72d8ce83d24517a&amp;token=490072482&amp;lang=zh_CN&amp;scene=21#wechat_redirect" target="_blank" rel="noreferrer">9种分布式ID生成方式</a>》。</p><p>为了让上手更加简单，ApacheShardingSphere 内置了<code>UUID</code>、<code>SNOWFLAKE</code> 两种分布式主键⽣成器，默认使⽤雪花算法（<code>snowflake</code>）⽣成64bit的⻓整型数据。不仅如此它还抽离出分布式主键⽣成器的接口，⽅便我们实现⾃定义的⾃增主键⽣成算法。</p><h2 id="_8、广播表" tabindex="-1">8、广播表 <a class="header-anchor" href="#_8、广播表" aria-label="Permalink to &quot;8、广播表&quot;">​</a></h2><p>广播表：存在于所有的分片数据源中的表，表结构和表中的数据在每个数据库中均完全一致。一般是为字典表或者配置表 <code>t_config</code>，某个表一旦被配置为广播表，只要修改某个数据库的广播表，所有数据源中广播表的数据都会跟着同步。</p><h2 id="_9、绑定表" tabindex="-1">9、绑定表 <a class="header-anchor" href="#_9、绑定表" aria-label="Permalink to &quot;9、绑定表&quot;">​</a></h2><p>绑定表：那些分片规则一致的主表和子表。比如：<code>t_order</code> 订单表和 <code>t_order_item</code> 订单服务项目表，都是按 <code>order_id</code> 字段分片，因此两张表互为绑定表关系。</p><p>那绑定表存在的意义是啥呢？</p><p>通常在我们的业务中都会使用 <code>t_order</code> 和 <code>t_order_item</code> 等表进行多表联合查询，但由于分库分表以后这些表被拆分成N多个子表。如果不配置绑定表关系，会出现笛卡尔积关联查询，将产生如下四条<code>SQL</code>。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> t_order_0 o </span><span style="color:#F78C6C;">JOIN</span><span style="color:#A6ACCD;"> t_order_item_0 i </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> o.order_id</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">i.order_id </span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> t_order_0 o </span><span style="color:#F78C6C;">JOIN</span><span style="color:#A6ACCD;"> t_order_item_1 i </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> o.order_id</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">i.order_id </span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> t_order_1 o </span><span style="color:#F78C6C;">JOIN</span><span style="color:#A6ACCD;"> t_order_item_0 i </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> o.order_id</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">i.order_id </span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> t_order_1 o </span><span style="color:#F78C6C;">JOIN</span><span style="color:#A6ACCD;"> t_order_item_1 i </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> o.order_id</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">i.order_id</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206051600349.png" alt="image-20220605160041310" style="zoom:80%;"><p>而配置绑定表关系后再进行关联查询时，只要对应表分片规则一致产生的数据就会落到同一个库中，那么只需 <code>t_order_0</code> 和 <code>t_order_item_0</code> 表关联即可。</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> t_order_0 o </span><span style="color:#F78C6C;">JOIN</span><span style="color:#A6ACCD;"> t_order_item_0 i </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> o.order_id</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">i.order_id </span></span>
<span class="line"><span style="color:#F78C6C;">SELECT</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">FROM</span><span style="color:#A6ACCD;"> t_order_1 o </span><span style="color:#F78C6C;">JOIN</span><span style="color:#A6ACCD;"> t_order_item_1 i </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> o.order_id</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">i.order_id</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206051600958.png" alt="image-20220605160052921" style="zoom:80%;"><blockquote><p><strong>注意</strong>：在关联查询时 <code>t_order</code> 它作为整个联合查询的主表。所有相关的路由计算都只使用主表的策略，<code>t_order_item</code> 表的分片相关的计算也会使用 <code>t_order</code> 的条件，所以要保证绑定表之间的分片键要完全相同。</p></blockquote><h1 id="数据异构方案" tabindex="-1">数据异构方案 <a class="header-anchor" href="#数据异构方案" aria-label="Permalink to &quot;数据异构方案&quot;">​</a></h1><p>何谓数据异构，上周交易部门商品的同事过来做分享，又看到这个词，他的PPT里面是 数据库异构。其实我们以前做的事情，也是可以称之为数据异构。比如我们将DB里面的数据持久化到Redis里面去，就是一种数据异构的方式。</p><p>如果要下个定义的话：<strong>把数据按需（数据结构、存取方式、存取形式）异地构建存储。</strong></p><h2 id="常见应用场景" tabindex="-1">常见应用场景 <a class="header-anchor" href="#常见应用场景" aria-label="Permalink to &quot;常见应用场景&quot;">​</a></h2><p>分库分表中有一个最为常见的场景，为了提升数据库的查询能力，我们都会对数据库做分库分表操作。比如订单库，开始的时候我们是按照订单ID维度去分库分表，那么后来的业务需求想按照商家维度去查询，比如我想查询某一个商家下的所有订单，就非常麻烦。</p><p>这个时候通过数据异构就能很好的解决此问题，如下图：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210292033284.png" alt="image-20221029203317187" style="zoom:80%;"><p><strong>数据异构总结起来大概有以下几种场景</strong></p><ol><li>数据库镜像</li><li>数据库实时备份</li><li>多级索引</li><li>search build（比如分库分表后的多维度数据查询）</li><li>业务cache刷新</li><li>价格、库存变化等重要业务消息</li></ol><h2 id="数据异构方向" tabindex="-1">数据异构方向 <a class="header-anchor" href="#数据异构方向" aria-label="Permalink to &quot;数据异构方向&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210292051803.png" alt="image-20221029205142744" style="zoom:80%;"><p>在日常业务开发中大致可以分为以上几种数据去向，<strong>DB-DB</strong>这种方式，一般常见于分库分表后，聚合查询的时候，比如我们按照订单ID去分库分表，那么这个时候我们要按照用户ID去查询，查询这个用户下面的订单就非常不方便了，当然可以使用统一加到内存中去，但这样不太好。</p><p>所以我们就可以用数据库异构的方式，重新按照用户ID的维度来分一个表，像在上面常见应用场景中介绍的那样。把数据异构到redis、elasticserach、slor中去要解决的问题跟按照多维度来查询的需求差不多。这些存储天生都有聚合的功能。当然同时也可以提高查询性能，应对大访问量，比如redis这种抗量银弹。</p><h2 id="数据异构的常用方法" tabindex="-1">数据异构的常用方法 <a class="header-anchor" href="#数据异构的常用方法" aria-label="Permalink to &quot;数据异构的常用方法&quot;">​</a></h2><h3 id="_1-完整克隆" tabindex="-1">1. 完整克隆 <a class="header-anchor" href="#_1-完整克隆" aria-label="Permalink to &quot;1. 完整克隆&quot;">​</a></h3><p>这个很简单就是将数据库A，全部拷贝一份到数据库B，这样的使用场景是离线统计跑任务脚本的时候可以。缺点也很突出，不适用于持续增长的数据。</p><h3 id="_2-标记同步" tabindex="-1">2. 标记同步 <a class="header-anchor" href="#_2-标记同步" aria-label="Permalink to &quot;2. 标记同步&quot;">​</a></h3><p>这个是业务场景比较简单的时候，理想情况下数据不会发生改变，比如日志数据，这个时候可以去标记，比如时间戳，这样当发生故障的时候还可以回溯到上一次同步点，开始重新同步数据。</p><h3 id="_3-binlog方式" tabindex="-1">3. binlog方式 <a class="header-anchor" href="#_3-binlog方式" aria-label="Permalink to &quot;3. binlog方式&quot;">​</a></h3><p>通过实时的订阅MySQL的binlog日志，消费到这些日志后，重新构建数据结构插入一个新的数据库或者是其他存储比如es、slor等等。订阅binlog日志可以比较好的能保证数据的一致性。</p><h3 id="_4-mq方式" tabindex="-1">4. MQ方式 <a class="header-anchor" href="#_4-mq方式" aria-label="Permalink to &quot;4. MQ方式&quot;">​</a></h3><p>业务数据写入DB的同时，也发送MQ一份，也就是业务里面实现双写。这种方式比较简单，但也很难保证数据一致性，对简单的业务场景可以采用这种方式。</p><h2 id="binlog方式" tabindex="-1">binlog方式 <a class="header-anchor" href="#binlog方式" aria-label="Permalink to &quot;binlog方式&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210292054713.png" alt="image-20221029205423641" style="zoom:80%;"><p>binglog是数据的日志记录方式，每次对数据的操作都会有binlog日志。现在开源的订阅binlog日志的组件，比如使用比较广泛的canal，它是阿里开源的基于mysql数据库binlog的增量订阅和消费组件。</p><p>由于cannal服务器目前读取的binlog事件只保存在内存中，并且只有一个canal客户端可以进行消费。所以如果需要多个消费客户端，可以引入activemq或者kafka。如上图绿色虚线框部分。</p><p>我们还需要确保全量对比来保证数据的一致性（canal+mq的重试机制基本可以保证写入异构库之后的数据一致性），这个时候可以有一个全量同步WORKER程序来保证，如上图深绿色部分。</p><h3 id="canal的工作原理" tabindex="-1">canal的工作原理 <a class="header-anchor" href="#canal的工作原理" aria-label="Permalink to &quot;canal的工作原理&quot;">​</a></h3><p>先来看下mysql主备（主从）复制原理如下图，在此原理基础之上我们再来理解canal的实现原理就一眼能明白了。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210292054721.png" alt="image-20221029205458647" style="zoom:80%;"><p><strong>mysql主备（主从）复制原理，从上层来看，复制分成三步：</strong></p><ol><li>master将改变记录到二进制日志(binary log)中（这些记录叫做二进制日志事件，binary log events，可以通过show binlog events进行查看）；</li><li>slave将master的binary log events拷贝到它的中继日志(relay log)；</li><li>slave重做中继日志中的事件，将改变反映它自己的数据。</li></ol><p>再来看下canal的原理，如下图：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210292055007.png" alt="image-20221029205516928" style="zoom:80%;"><p><strong>cannal实现原理相对比较简单（参照上面的mysql主备复制实现原理）：</strong></p><ol><li>canal模拟mysql slave的交互协议，伪装自己为mysql slave，向mysql master发送dump协议</li><li>mysql master收到dump请求，开始推送binary log给slave(也就是canal)</li><li>canal解析binary log对象(原始为byte流)</li></ol><p>我们在部署canal server的时候要部署多台，来保证高可用。但是canal的原理，是只有一台服务器在跑处理，其它的服务器作为热备。canal server的高可用是通过zookeeper来维护的。</p><p><strong>有关canal更具体的使用和详细原理请参照：<a href="https://github.com/alibaba/canal" target="_blank" rel="noreferrer">https://github.com/alibaba/canal</a></strong></p><h3 id="注意点" tabindex="-1">注意点 <a class="header-anchor" href="#注意点" aria-label="Permalink to &quot;注意点&quot;">​</a></h3><ul><li>确认MySQL开启binlog，使用<strong>show variables like &#39;log_bin&#39;;</strong> 查看ON为已开启</li><li>确认目标库可以产生binlog，<strong>show master status</strong> 注意Binlog_Do_DB，Binlog_Ignore_DB参数</li><li>确认binlog格式为ROW，使用<strong>show variables like &#39;binlog_format&#39;;</strong> 非ROW模式登录MySQL执行 <strong>set global binlog_format=ROW; flush logs;</strong> 或者通过更改MySQL配置文件并重启MySQL生效。</li><li>为保证binlake服务可以获取Binlog，需添加授权，执行 <strong>GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO &#39;admin&#39;@&#39;%&#39; identified by &#39;admin&#39;; FLUSH PRIVILEGES;</strong></li></ul><h2 id="mq方式" tabindex="-1">MQ方式 <a class="header-anchor" href="#mq方式" aria-label="Permalink to &quot;MQ方式&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210292055583.png" alt="image-20221029205536514" style="zoom:80%;"><p>mq的方式，就相对简单，实际上是在业务逻辑中写DB的同时去写一次MQ，但是这种方式不能够保证数据一致性，就是不能保证跨资源的事务。<strong>注：调用第三方远程RPC的操作一定不要放到事务中。</strong></p><h2 id="总结" tabindex="-1">总结 <a class="header-anchor" href="#总结" aria-label="Permalink to &quot;总结&quot;">​</a></h2><p>本文主要叙述了数据异构的使用场景，方法。这里面涉及到的activemq以及canal并没有深入分析，关于这块的内容可以直接参考相关具体文档，文中已给了链接地址。</p><p>根据数据异构的定义，将数据异地构建存储，我们可以应用的地方就非常多，文中说的分库分表之后按照其它维度来查询的时候，我们想脱离DB直接用缓存比如redis来抗量的时候。数据异构这种方式都能够很好的帮助我们来解决诸如此类的问题。</p><h1 id="查询分离方案" tabindex="-1">查询分离方案 <a class="header-anchor" href="#查询分离方案" aria-label="Permalink to &quot;查询分离方案&quot;">​</a></h1><p>在之前一篇文章中提到过对于业务主表读写缓慢的解决方案：<strong>冷热分离</strong>。冷热分离固然是一个性价比高的解决方案，但也并不是银弹，仍然有诸多限制，比如：</p><ol><li>查询冷数据慢</li><li>业务无法修改冷数据</li><li>冷数据多到一定程度系统依旧扛不住</li></ol><p>此时如果需要解决以上问题，可以采用另外一种方案：使用 <strong>查询分离</strong> 优化业务主表数据大查询缓慢的问题</p><p>本篇文章介绍了表数据量大查询缓慢的一种解决方案：查询分离，但这也不是银弹，仍然是存在一些不足，比如表数据量大，写入缓慢怎么办？当然查询分离还有一个重要的问题：历史数据如何迁移？这个处理也是非常简单，但是也有许多需要考虑的点。</p><h2 id="什么是查询分离" tabindex="-1">什么是查询分离？ <a class="header-anchor" href="#什么是查询分离" aria-label="Permalink to &quot;什么是查询分离？&quot;">​</a></h2><p>查询分离从字面上来说非常容易理解，其实就是在写数据时保存一个备份数据到另外的存储系统，在查询时直接从另外的存储系统中获取数据，如下图：</p><blockquote><p>Tip：查询分离和读写分离是有区别的，读写分离数据库类型是相同的，比如都是MySQL库。读写分离是通过数据库的主从复制的方式来同步数据，通过让主数据库负责事务性的增删改，而从数据库负责非事务性的查询操作来提升数据库的并发负载能力。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210291918603.png" alt="image-20221029191821504" style="zoom:50%;"><p>以上只是简单的架构图，其中有些细节还是需要深究，如下：</p><ol><li>什么时候触发查询分离？</li><li>如何实现查询分离？</li><li>查询数据的存储系统选型？</li><li>查询数据如何使用？</li></ol><h2 id="查询分离的适用场景" tabindex="-1">查询分离的适用场景 <a class="header-anchor" href="#查询分离的适用场景" aria-label="Permalink to &quot;查询分离的适用场景&quot;">​</a></h2><p>当你在实际业务中遇到以下情形，则可以考虑使用查询分离解决方案。</p><ul><li>数据量大，单表数据达到千万；</li><li>查询速度慢，即使加了索引也是很慢；</li><li>存在复杂的表关联查询；</li><li>所有写数据的请求效率尚可；</li><li>查询数据的请求效率很低；</li><li>所有的数据任何时候都可能被修改；</li><li>业务希望我们优化查询数据的功能。</li></ul><p>曾做过 SaaS 客服系统的架构优化，系统里有一个工单查询功能，工单表中存放了几千万条数据，且查询工单表数据时需要关联十几个子表，每个子表的数据也是超亿条。</p><p>面对如此庞大的数据量，跟前面的冷热分离一样，每次客户查询数据时几十秒才能返回结果，即便我们使用了索引、SQL 等数据库优化技巧，效果依然不明显。</p><p>工单表中有些数据是几年前的，客户说这些数据涉及诉讼问题，需要继续保持更新，因此我们无法将这些旧数据封存到别的地方，也就没法通过前面的冷热分离方案来解决。</p><p>最终我们采用了查询分离的解决方案，才得以将这个问题顺利解决：将更新的数据放在一个数据库里，而查询的数据放在另外一个系统里。因为数据的更新都是单表更新，不需要关联也没有外键，所以更新速度立马得到提升，每次客户查询数据时，500ms 内就可得到返回结果。</p><h2 id="什么时候触发查询分离" tabindex="-1">什么时候触发查询分离 <a class="header-anchor" href="#什么时候触发查询分离" aria-label="Permalink to &quot;什么时候触发查询分离&quot;">​</a></h2><p>简单的来说就是什么时候应该保存一份数据到查询数据库中，其实也就是数据异构的过程，详细文章可以看我前面一篇文章：<a href="https://mp.weixin.qq.com/s?__biz=MzU3MDAzNDg1MA==&amp;mid=2247518212&amp;idx=1&amp;sn=5311d102f977427ca4919dedbb37f94b&amp;chksm=fcf757c9cb80dedf0ce7c0ccdc7d177c22614e0d27a85ee1bd782523552e15b1dfc846934fd8&amp;token=238513143&amp;lang=zh_CN&amp;scene=21#wechat_redirect" target="_blank" rel="noreferrer">数据异构就该这样做，yyds~</a></p><p>这里介绍三种方式，如下：</p><ol><li>同步建立</li><li>异步建立</li><li>binlog方式</li></ol><h3 id="_1、-同步建立" tabindex="-1">1、 同步建立 <a class="header-anchor" href="#_1、-同步建立" aria-label="Permalink to &quot;1、 同步建立&quot;">​</a></h3><p>修改业务代码：在写入常规数据后，同步建立查询数据。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210291921151.png" alt="image-20221029192145040" style="zoom:50%;"><p>该种方案优缺点也非常明显：</p><p><strong>优点</strong>：查询数据的一致性和实时性得到了保证</p><p><strong>缺点</strong>：业务代码侵入比较强；减缓写操作的效率</p><h3 id="_2、-异步建立" tabindex="-1">2、 异步建立 <a class="header-anchor" href="#_2、-异步建立" aria-label="Permalink to &quot;2、 异步建立&quot;">​</a></h3><p>修改业务代码：写入数据后，异步建立查询数据</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210291922361.png" alt="image-20221029192207247" style="zoom:50%;"><p>该种方案的优缺点如下：</p><p><strong>优点</strong>：不影响主流程</p><p><strong>缺点</strong>：数据一致性存在问题</p><h3 id="_3、-binlog的方式" tabindex="-1">3、 binlog的方式 <a class="header-anchor" href="#_3、-binlog的方式" aria-label="Permalink to &quot;3、 binlog的方式&quot;">​</a></h3><p>该种方案也是业界常用的一种方案，对于代码是无侵入的，通过监听数据库日志的方式建立查询数据，如下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202210291922038.png" alt="image-20221029192252921" style="zoom:50%;"><p>该种方案的优缺点如下：</p><p><strong>优点</strong>：不影响主流程；代码侵入为0</p><p><strong>缺点</strong>：数据一致性存在问题；架构相对复杂</p><h2 id="如何实现查询分离" tabindex="-1">如何实现查询分离 <a class="header-anchor" href="#如何实现查询分离" aria-label="Permalink to &quot;如何实现查询分离&quot;">​</a></h2><p>对于上述三种方案都算是比较常见的方案，对于第一种同步的方式比较简单，这里不再介绍；对于第三种binlog的方式在数据异构的文章中介绍过，详情见：<a href="https://mp.weixin.qq.com/s?__biz=MzU3MDAzNDg1MA==&amp;mid=2247518212&amp;idx=1&amp;sn=5311d102f977427ca4919dedbb37f94b&amp;chksm=fcf757c9cb80dedf0ce7c0ccdc7d177c22614e0d27a85ee1bd782523552e15b1dfc846934fd8&amp;token=238513143&amp;lang=zh_CN&amp;scene=21#wechat_redirect" target="_blank" rel="noreferrer">数据异构就该这样做，yyds~</a></p><p>这篇文章来介绍一下异步的方式，异步的方式有很多，可以放在内存中进行操作，但是这有些弊端：</p><ol><li>数据过多，内存有限</li><li>服务重启，内存数据将会丢失</li></ol><p>因此最终我们可以选择<strong>MQ</strong>的方式，那么此时就涉及到了MQ的技术选型，这里给两个建议：</p><ol><li>如果你的公司已经用了MQ，那么直接接着用即可</li><li>如果公司目前未引入MQ，则需要架构组考量选型了</li></ol><p>当然一旦引入了MQ还需要考虑的问题很多，如下：</p><h3 id="_1、-mq突然宕机了怎么办" tabindex="-1">1、 MQ突然宕机了怎么办？ <a class="header-anchor" href="#_1、-mq突然宕机了怎么办" aria-label="Permalink to &quot;1、 MQ突然宕机了怎么办？&quot;">​</a></h3><p>MQ宕机意味着查询数据不能继续建立了，我们可以在写入数据的同时给该条数据加一个标志字段（已搬运、未搬运），当MQ启动后，查询所有未搬运的数据，继续建立查询数据</p><blockquote><p>这里的方案很多，按照业务实际情况考量</p></blockquote><h3 id="_2、消息的幂等消费" tabindex="-1">2、消息的幂等消费 <a class="header-anchor" href="#_2、消息的幂等消费" aria-label="Permalink to &quot;2、消息的幂等消费&quot;">​</a></h3><p>消息的幂等消费一定要保证，避免数据重复建立，比如：主数据的订单 A 更新后，我们在查询数据中插入了 A，可是此时系统出问题了，系统误以为查询数据没更新，又把订单 A 插入更新了一次。</p><h3 id="_3、消息的时序性问题" tabindex="-1">3、消息的时序性问题 <a class="header-anchor" href="#_3、消息的时序性问题" aria-label="Permalink to &quot;3、消息的时序性问题&quot;">​</a></h3><p>比如某个订单 A 更新了 1 次数据变成 A1，线程甲将 A1 的数据搬到查询数据中。不一会儿，后台订单 A 又更新了 1 次数据变成 A2，线程乙也启动工作，将 A2 的数据搬到查询数据中。</p><p>所谓的时序性就是如果线程甲启动比乙早，但搬运数据动作比线程乙还晚完成，就有可能出现查询数据最终变成过期的 A1</p><h2 id="查询数据的存储系统选型" tabindex="-1">查询数据的存储系统选型？ <a class="header-anchor" href="#查询数据的存储系统选型" aria-label="Permalink to &quot;查询数据的存储系统选型？&quot;">​</a></h2><p>既然为了解决表数据量大查询缓慢的问题，肯定是不能选用关系型数据库了，那么还有其他选择吗？</p><p>内存数据库虽然性能非常高，比如Redis，但是不适合海量数据，太费钱了</p><p>那么这里比较适用的有如下三种：</p><ol><li>MongoDB</li><li>HBase</li><li>Elasticsearch</li></ol><p>这里选型还是要根据自己公司业务选择，如果已经有在用的，则直接用即可；另外就是选择自己熟悉的，比如当初我们设计架构方案时，为什么选择用 Elasticsearch，除 ES 对查询的扩展性支持外，最关键的一点是我们团队对 Elasticsearch 很熟悉。</p><h2 id="查询数据如何使用" tabindex="-1">查询数据如何使用？ <a class="header-anchor" href="#查询数据如何使用" aria-label="Permalink to &quot;查询数据如何使用？&quot;">​</a></h2><p>查询数据很简单，每个数据库都有对应的API，直接调用查询</p><p>但是，这里有一个问题：<strong>数据查询更新完前，查询数据不一致怎么办？</strong>，给出两种方案：</p><p>在查询数据更新到最新前，不允许用户查询。（我们没用过这种设计，但我确实见过市面上有这样的设计。）</p><p>给用户提示：您目前查询到的数据可能是 1 秒前的数据，如果发现数据不准确，可以尝试刷新一下，这种提示用户一般比较容易接受。</p><h1 id="高性能架构模式⭐⭐" tabindex="-1">高性能架构模式⭐⭐ <a class="header-anchor" href="#高性能架构模式⭐⭐" aria-label="Permalink to &quot;高性能架构模式⭐⭐&quot;">​</a></h1><p>互联网业务兴起之后，海量用户加上海量数据的特点，单个数据库服务器已经难以满足业务需要，必须考虑数据库集群的方式来提升性能。高性能数据库集群的<code>第一种方式是“读写分离”</code>，<code>第二种方式是“数据库分片”</code>。</p><h2 id="_1、读写分离架构" tabindex="-1">1、读写分离架构 <a class="header-anchor" href="#_1、读写分离架构" aria-label="Permalink to &quot;1、读写分离架构&quot;">​</a></h2><p>**读写分离原理：**读写分离的基本原理是将数据库读写操作分散到不同的节点上，下面是其基本架构图：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804217.jpg" alt="img" style="zoom:80%;"><p><strong>读写分离的基本实现</strong></p><blockquote><ul><li>主库负责处理事务性的增删改操作，从库负责处理查询操作，能够有效的避免由数据更新导致的行锁，使得整个系统的查询性能得到极大的改善。</li><li>读写分离是根据 SQL 语义的分析，将读操作和写操作分别路由至主库与从库。</li><li>通过<code>一主多从</code>的配置方式，可以将查询请求均匀的分散到多个数据副本，能够进一步的提升系统的处理能力。</li><li>使用<code>多主多从</code>的方式，不但能够提升系统的吞吐量，还能够提升系统的可用性，可以达到在任何一个数据库宕机，甚至磁盘物理损坏的情况下仍然不影响系统的正常运行。</li></ul></blockquote><p><strong>下图展示了根据业务需要，将用户表的写操作和读操路由到不同的数据库的方案：</strong></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804223.png" alt="image-20220804223138651" style="zoom:80%;"><h2 id="_2、cap-理论" tabindex="-1">2、CAP 理论 <a class="header-anchor" href="#_2、cap-理论" aria-label="Permalink to &quot;2、CAP 理论&quot;">​</a></h2><p>CAP 定理（CAP theorem）又被称作布鲁尔定理（Brewer&#39;s theorem），是加州大学伯克利分校的计算机科学家埃里克·布鲁尔（Eric Brewer）在 2000 年的 ACM PODC 上提出的一个猜想。对于设计分布式系统的架构师来说，CAP 是必须掌握的理论。</p><h3 id="_1-cap分析" tabindex="-1">1 CAP分析 <a class="header-anchor" href="#_1-cap分析" aria-label="Permalink to &quot;1 CAP分析&quot;">​</a></h3><p>在一个<code>分布式系统中</code>，当涉及读写操作时，只能保证一致性（Consistence）、可用性（Availability）、分区容错性（Partition Tolerance）三者中的两个，另外一个必须被牺牲。</p><blockquote><ul><li>C 一致性（Consistency）：对某个指定的客户端来说，读操作保证能够返回最新的写操作结果</li><li>A 可用性（Availability）：非故障的节点在合理的时间内返回合理的响应（不是错误和超时的响应）</li><li>P 分区容忍性（Partition Tolerance）：当出现网络分区后（可能是丢包，也可能是连接中断，还可能是拥塞），系统能够继续“履行职责”</li></ul></blockquote><h3 id="_2-cap特点" tabindex="-1">2 CAP特点 <a class="header-anchor" href="#_2-cap特点" aria-label="Permalink to &quot;2 CAP特点&quot;">​</a></h3><blockquote><p>在实际设计过程中，每个系统不可能只处理一种数据，而是包含多种类型的数据，有的数据必须选择 CP，有的数据必须选择 AP，分布式系统理论上不可能选择 CA 架构。</p></blockquote><blockquote><p>CP：如下图所示，为了保证一致性，当发生分区现象后，N1 节点上的数据已经更新到 y，但由于 N1 和 N2 之间的复制通道中断，数据 y 无法同步到 N2，N2 节点上的数据还是 x。这时客户端 C 访问 N2 时，N2 需要返回 Error，提示客户端 C“系统现在发生了错误”，这种处理方式`违背了可用性（Availability）的要求，因此 CAP 三者只能满足 CP。</p></blockquote><blockquote><p>AP：如下图所示，为了保证可用性，当发生分区现象后，N1 节点上的数据已经更新到 y，但由于 N1 和 N2 之间的复制通道中断，数据 y 无法同步到 N2，N2 节点上的数据还是 x。这时客户端 C 访问 N2 时，N2 将当前自己拥有的数据 x 返回给客户端 C 了，而实际上当前最新的数据已经是 y 了，这就不满足一致性（Consistency）的要求了，因此 CAP 三者只能满足 AP。</p></blockquote><blockquote><p>注意：这里 N2 节点返回 x，虽然不是一个“正确”的结果，但是一个“合理”的结果，因为 x 是旧的数据，并不是一个错乱的值，只是不是最新的数据而已。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251611935.png" alt="img" style="zoom:80%;"><p>CAP 理论中的 <code>C 在实践中是不可能完美实现的</code>，在数据复制的过程中，节点N1 和节点 N2 的数据并不一致（强一致性）。即使无法做到<code>强一致性</code>，但应用可以采用适合的方式达到<code>最终一致性</code>。具有如下特点：</p><blockquote><ul><li>基本可用（Basically Available）：分布式系统在出现故障时，允许损失部分可用性，即保证核心可用。</li><li>软状态（Soft State）：允许系统存在中间状态，而该中间状态不会影响系统整体可用性。这里的中间状态就是 CAP 理论中的数据不一致。</li><li>最终一致性（Eventual Consistency）：系统中的所有数据副本经过一定时间后，最终能够达到一致的状态。</li></ul></blockquote><h2 id="_3、数据库分片架构" tabindex="-1">3、数据库分片架构 <a class="header-anchor" href="#_3、数据库分片架构" aria-label="Permalink to &quot;3、数据库分片架构&quot;">​</a></h2><p><strong>读写分离的问题</strong></p><blockquote><p>读写分离分散了数据库读写操作的压力，但没有分散存储压力，为了满足业务数据存储的需求，就需要将存储分散到多台数据库服务器上。</p></blockquote><p><strong>数据分片</strong></p><blockquote><p>将存放在单一数据库中的数据分散地存放至多个数据库或表中，以达到提升性能瓶颈以及可用性的效果。 数据分片的有效手段是对关系型数据库进行分库和分表。数据分片的拆分方式又分为垂直分片和水平分片。</p></blockquote><h3 id="_1、垂直分片" tabindex="-1">1、垂直分片 <a class="header-anchor" href="#_1、垂直分片" aria-label="Permalink to &quot;1、垂直分片&quot;">​</a></h3><p><strong>垂直分库</strong></p><blockquote><p>按照业务拆分的方式称为垂直分片，又称为纵向拆分，它的核心理念是<strong>专库专用</strong>。 在拆分之前，一个数据库由多个数据表构成，每个表对应着不同的业务。而拆分之后，则是按照业务将表进行归类，分布到不同的数据库中，从而将压力分散至不同的数据库。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804228.jpg" alt="img" style="zoom:25%;"><p>下图展示了根据业务需要，将用户表和订单表垂直分片到不同的数据库的方案：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804233.png" alt="image-20220804221855449" style="zoom:80%;"><blockquote><p>垂直拆分可以缓解数据量和访问量带来的问题，但无法根治。如果垂直拆分之后，表中的数据量依然超过单节点所能承载的阈值，则需要水平分片来进一步处理。</p></blockquote><p><strong>垂直分表</strong></p><p>垂直分表适合将表中某些不常用的列，或者是占了大量空间的列拆分出去。</p><p>假设我们是一个婚恋网站，用户在筛选其他用户的时候，主要是用 age 和 sex 两个字段进行查询，而 nickname 和 description 两个字段主要用于展示，一般不会在业务查询中用到。description 本身又比较长，因此我们可以将这两个字段独立到另外一张表中，这样在查询 age 和 sex 时，就能带来一定的性能提升。</p><p>垂直分表引入的复杂性主要体现在表操作的数量要增加。例如，原来只要一次查询就可以获取 name、age、sex、nickname、description，现在需要两次查询，一次查询获取 name、age、sex，另外一次查询获取 nickname、description。</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804225.jpg" alt="img"></p><p>水平分表适合表行数特别大的表，水平分表属于水平分片。</p><h3 id="_2、水平分片" tabindex="-1">2、水平分片 <a class="header-anchor" href="#_2、水平分片" aria-label="Permalink to &quot;2、水平分片&quot;">​</a></h3><blockquote><p>水平分片又称为横向拆分。相对于垂直分片，它不再将数据根据业务逻辑分类，而是通过某个字段（或某几个字段），根据某种规则将数据分散至多个库或表中，每个分片仅包含数据的一部分。 例如：根据主键分片，偶数主键的记录放入 0 库（或表），奇数主键的记录放入 1 库（或表），如下图所示。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804240.png" alt="image-20220804222212087" style="zoom:80%;"><p>单表进行切分后，是否将多个表分散在不同的数据库服务器中，可以根据实际的切分效果来确定。</p><blockquote><ul><li><p>**水平分表：**单表切分为多表后，新的表即使在同一个数据库服务器中，也可能带来可观的性能提升，如果性能能够满足业务要求，可以不拆分到多台数据库服务器，毕竟业务分库也会引入很多复杂性；</p></li><li><p>**水平分库：**如果单表拆分为多表后，单台服务器依然无法满足性能要求，那就需要将多个表分散在不同的数据库服务器中。</p></li></ul></blockquote><blockquote><p><strong>阿里巴巴Java开发手册</strong></p><p>【推荐】单表行数超过 500 万行或者单表容量超过 2GB，才推荐进行分库分表。</p><p>说明：如果预计三年后的数据量根本达不到这个级别，请不要在创建表时就分库分表。</p></blockquote><h2 id="_4、读写分离和数据分片架构" tabindex="-1">4、读写分离和数据分片架构 <a class="header-anchor" href="#_4、读写分离和数据分片架构" aria-label="Permalink to &quot;4、读写分离和数据分片架构&quot;">​</a></h2><p>下图展现了将数据分片与读写分离一同使用时，应用程序与数据库集群之间的复杂拓扑关系。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804257.png" alt="image-20220804223321167" style="zoom:80%;"><h2 id="_5、实现方式" tabindex="-1">5、实现方式 <a class="header-anchor" href="#_5、实现方式" aria-label="Permalink to &quot;5、实现方式&quot;">​</a></h2><p>读写分离和数据分片具体的实现方式一般有两种： <code>程序代码封装</code>和<code>中间件封装</code>。</p><h3 id="_1、程序代码封装" tabindex="-1">1、程序代码封装 <a class="header-anchor" href="#_1、程序代码封装" aria-label="Permalink to &quot;1、程序代码封装&quot;">​</a></h3><p>程序代码封装指在代码中抽象一个<code>数据访问层（或中间层封装）</code>，实现读写操作分离和数据库服务器连接的管理。</p><p>**其基本架构是：**以读写分离为例</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804350.jpg" alt="img" style="zoom:67%;"><h3 id="_2、中间件封装" tabindex="-1">2、中间件封装 <a class="header-anchor" href="#_2、中间件封装" aria-label="Permalink to &quot;2、中间件封装&quot;">​</a></h3><p>中间件封装指的是<code>独立一套系统出来</code>，实现读写操作分离和数据库服务器连接的管理。对于业务服务器来说，访问中间件和访问数据库没有区别，在业务服务器看来，中间件就是一个数据库服务器。</p><p>**基本架构是：**以读写分离为例</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804997.jpg" alt="img" style="zoom:80%;"><h3 id="_3、常用解决方案" tabindex="-1">3、常用解决方案 <a class="header-anchor" href="#_3、常用解决方案" aria-label="Permalink to &quot;3、常用解决方案&quot;">​</a></h3><blockquote><p>Apache ShardingSphere（程序级别和中间件级别）</p><p>MyCat（数据库中间件）</p></blockquote><h1 id="shardingsphere-1" tabindex="-1">ShardingSphere <a class="header-anchor" href="#shardingsphere-1" aria-label="Permalink to &quot;ShardingSphere&quot;">​</a></h1><h2 id="_1、简介-1" tabindex="-1">1、简介 <a class="header-anchor" href="#_1、简介-1" aria-label="Permalink to &quot;1、简介&quot;">​</a></h2><p>官网：<a href="https://shardingsphere.apache.org/index_zh.html" target="_blank" rel="noreferrer">https://shardingsphere.apache.org/index_zh.html</a></p><p>文档：<a href="https://shardingsphere.apache.org/document/5.1.1/cn/overview/" target="_blank" rel="noreferrer">https://shardingsphere.apache.org/document/5.1.1/cn/overview/</a></p><p>Apache ShardingSphere 由 JDBC、Proxy 和 Sidecar（规划中）这 3 款既能够独立部署，又支持混合部署配合使用</p><h2 id="_2、shardingsphere-jdbc-1" tabindex="-1">2、ShardingSphere-JDBC <a class="header-anchor" href="#_2、shardingsphere-jdbc-1" aria-label="Permalink to &quot;2、ShardingSphere-JDBC&quot;">​</a></h2><p><strong>程序代码封装</strong></p><blockquote><p>定位为轻量级 Java 框架，在 Java 的 JDBC 层提供的额外服务。 它使用客户端直连数据库，以 jar 包形式提供服务，无需额外部署和依赖，可理解为增强版的 JDBC 驱动，完全兼容 JDBC 和各种 ORM 框架。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804017.png" alt="image-20220804195402870" style="zoom:80%;"><h2 id="_3、shardingsphere-proxy" tabindex="-1">3、ShardingSphere-Proxy <a class="header-anchor" href="#_3、shardingsphere-proxy" aria-label="Permalink to &quot;3、ShardingSphere-Proxy&quot;">​</a></h2><p><strong>中间件封装</strong></p><p>定位为透明化的<code>数据库代理端</code>，提供封装了数据库二进制协议的服务端版本，用于完成对异构语言的支持。 目前提供 MySQL 和 PostgreSQL版本，它可以使用任何兼容 MySQL/PostgreSQL 协议的访问客户端（如：MySQL Command Client, MySQL Workbench, Navicat 等）操作数据，对 DBA 更加友好。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804029.png" alt="image-20220804195432673" style="zoom:80%;"><h1 id="mysql主从同步⭐" tabindex="-1">MySQL主从同步⭐ <a class="header-anchor" href="#mysql主从同步⭐" aria-label="Permalink to &quot;MySQL主从同步⭐&quot;">​</a></h1><h2 id="_1、mysql主从同步原理" tabindex="-1">1、MySQL主从同步原理 <a class="header-anchor" href="#_1、mysql主从同步原理" aria-label="Permalink to &quot;1、MySQL主从同步原理&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804041.png" alt="img" style="zoom:80%;"><p>**基本原理：**slave会从master读取binlog来进行数据同步</p><p><strong>具体步骤：</strong></p><ul><li><code>step1：</code>master将数据改变记录到<code>二进制日志（binary log）</code>中。</li><li><code>step2：</code> 当slave上执行 <code>start slave</code> 命令之后，slave会创建一个 <code>IO 线程</code>用来连接master，请求master中的binlog。</li><li><code>step3：</code>当slave连接master时，master会创建一个 <code>log dump 线程</code>，用于发送 binlog 的内容。在读取 binlog 的内容的操作中，会对主节点上的 binlog 加锁，当读取完成并发送给从服务器后解锁。</li><li><code>step4：</code>IO 线程接收主节点 binlog dump 进程发来的更新之后，保存到 <code>中继日志（relay log）</code> 中。</li><li><code>step5：</code>slave的<code>SQL线程</code>，读取relay log日志，并解析成具体操作，从而实现主从操作一致，最终数据一致。</li></ul><h2 id="_2、一主多从配置" tabindex="-1">2、一主多从配置 <a class="header-anchor" href="#_2、一主多从配置" aria-label="Permalink to &quot;2、一主多从配置&quot;">​</a></h2><p>服务器规划：使用<code>docker</code>方式创建，<code>主从服务器IP一致，端口号不一致</code></p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804054.png" alt="image-20220807183231101"></p><ul><li>主服务器：容器名<code>atguigu-mysql-master</code>，端口<code>3307</code></li><li>从服务器：容器名<code>atguigu-mysql-slave1</code>，端口<code>3308</code></li><li>从服务器：容器名<code>atguigu-mysql-slave2</code>，端口<code>3309</code></li></ul><p>**注意：**如果此时防火墙是开启的，<code>则先关闭防火墙，并重启docker</code>，否则后续安装的MySQL无法启动</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#关闭docker</span></span>
<span class="line"><span style="color:#FFCB6B;">systemctl</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">stop</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">docker</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#关闭防火墙</span></span>
<span class="line"><span style="color:#FFCB6B;">systemctl</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">stop</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">firewalld</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#启动docker</span></span>
<span class="line"><span style="color:#FFCB6B;">systemctl</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">start</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">docker</span></span></code></pre></div><h3 id="_1、准备主服务器" tabindex="-1">1、准备主服务器 <a class="header-anchor" href="#_1、准备主服务器" aria-label="Permalink to &quot;1、准备主服务器&quot;">​</a></h3><p><strong>step1：在docker中创建并启动MySQL主服务器：</strong><code>端口3307</code></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">run</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-p </span><span style="color:#F78C6C;">3307</span><span style="color:#C3E88D;">:3306</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/mysql/master/conf:/etc/mysql/conf.d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/mysql/master/data:/var/lib/mysql</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-e </span><span style="color:#C3E88D;">MYSQL_ROOT_PASSWORD=</span><span style="color:#F78C6C;">123456</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">--name </span><span style="color:#C3E88D;">atguigu-mysql-master</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql:8.0.29</span></span></code></pre></div><p><strong>step2：创建MySQL主服务器配置文件：</strong></p><p>默认情况下MySQL的binlog日志是自动开启的，可以通过如下配置定义一些可选配置</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/atguigu/mysql/master/conf/my.cnf</span></span></code></pre></div><p>配置如下内容</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">mysqld</span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 服务器唯一id，默认值1</span></span>
<span class="line"><span style="color:#A6ACCD;">server-id</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 设置日志格式，默认值ROW</span></span>
<span class="line"><span style="color:#A6ACCD;">binlog_format</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">STATEMENT</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 二进制日志名，默认binlog</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># log-bin=binlog</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 设置需要复制的数据库，默认复制全部数据库</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#binlog-do-db=mytestdb</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 设置不需要复制的数据库</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#binlog-ignore-db=mysql</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#binlog-ignore-db=infomation_schema</span></span></code></pre></div><p>重启MySQL容器</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">restart</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">atguigu-mysql-master</span></span></code></pre></div><p>binlog格式说明</p><ul><li>binlog_format=STATEMENT：日志记录的是主机数据库的<code>写指令</code>，性能高，但是now()之类的函数以及获取系统参数的操作会出现主从数据不同步的问题。</li><li>binlog_format=ROW（默认）：日志记录的是主机数据库的<code>写后的数据</code>，批量操作时性能较差，解决now()或者 user()或者 @@hostname 等操作在主从机器上不一致的问题。</li><li>binlog_format=MIXED：是以上两种level的混合使用，有函数用ROW，没函数用STATEMENT，但是无法识别系统变量</li></ul><p><code>binlog-ignore-db和binlog-do-db的优先级问题：</code></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804954.png" alt="img" style="zoom:80%;"><p><strong>step3：使用命令行登录MySQL主服务器</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#进入容器：env LANG=C.UTF-8 避免容器中显示中文乱码(可以在命令行输入和显示中文)</span></span>
<span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">exec</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-it</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">atguigu-mysql-master</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">env</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">LANG=C.UTF-8</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#进入容器内的mysql命令行</span></span>
<span class="line"><span style="color:#FFCB6B;">mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uroot</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#修改默认密码校验方式</span></span>
<span class="line"><span style="color:#FFCB6B;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">USER</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">IDENTIFIED</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">WITH</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">mysql_native_password</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">BY</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span></code></pre></div><p><strong>step4：主机中创建slave用户</strong></p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 创建slave用户</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">USER</span><span style="color:#A6ACCD;"> &#39;</span><span style="color:#82AAFF;">atguigu_slave</span><span style="color:#A6ACCD;">&#39;@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 设置密码</span></span>
<span class="line"><span style="color:#F78C6C;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">USER</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">atguigu_slave</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> IDENTIFIED </span><span style="color:#F78C6C;">WITH</span><span style="color:#A6ACCD;"> mysql_native_password </span><span style="color:#F78C6C;">BY</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 授予复制权限</span></span>
<span class="line"><span style="color:#F78C6C;">GRANT</span><span style="color:#A6ACCD;"> REPLICATION SLAVE </span><span style="color:#F78C6C;">ON</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">.</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TO</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">atguigu_slave</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 刷新权限</span></span>
<span class="line"><span style="color:#A6ACCD;">FLUSH PRIVILEGES;</span></span></code></pre></div><p><strong>step5：主机中查询master状态：</strong></p><p>执行完此步骤后<code>不要再操作主服务器MYSQL</code>，防止主服务器状态值变化</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">SHOW </span><span style="color:#F78C6C;">MASTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">STATUS</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><p>记下<code>File</code>和<code>Position</code>的值。执行完此步骤后不要再操作主服务器MYSQL，防止主服务器状态值变化。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251655718.png" alt="image-20221125165510607" style="zoom:80%;"><h3 id="_2、准备从服务器" tabindex="-1">2、准备从服务器 <a class="header-anchor" href="#_2、准备从服务器" aria-label="Permalink to &quot;2、准备从服务器&quot;">​</a></h3><p>可以配置多台从机slave1、slave2...，这里以配置slave1为例</p><p><strong>step1：在docker中创建并启动MySQL从服务器：</strong><code>端口3308，3309</code></p><p>第一台从服务器</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">run</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-p </span><span style="color:#F78C6C;">3308</span><span style="color:#C3E88D;">:3306</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/mysql/slave1/conf:/etc/mysql/conf.d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/mysql/slave1/data:/var/lib/mysql</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-e </span><span style="color:#C3E88D;">MYSQL_ROOT_PASSWORD=</span><span style="color:#F78C6C;">123456</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">--name </span><span style="color:#C3E88D;">atguigu-mysql-slave1</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql:8.0.29</span></span></code></pre></div><p>第二台从服务器</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">run</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-p </span><span style="color:#F78C6C;">3309</span><span style="color:#C3E88D;">:3306</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/mysql/slave2/conf:/etc/mysql/conf.d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/mysql/slave2/data:/var/lib/mysql</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-e </span><span style="color:#C3E88D;">MYSQL_ROOT_PASSWORD=</span><span style="color:#F78C6C;">123456</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">--name </span><span style="color:#C3E88D;">atguigu-mysql-slave2</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql:8.0.29</span></span></code></pre></div><p><strong>step2：创建MySQL从服务器配置文件：</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/atguigu/mysql/slave1/conf/my.cnf</span></span></code></pre></div><p>配置如下内容：</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">mysqld</span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 服务器唯一id，每台服务器的id必须不同，如果配置其他从机，注意修改id</span></span>
<span class="line"><span style="color:#A6ACCD;">server-id</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">2</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 中继日志名，默认xxxxxxxxxxxx-relay-bin</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#relay-log=relay-bin</span></span></code></pre></div><p>第二台服务器也是一样</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/atguigu/mysql/slave2/conf/my.cnf</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">mysqld</span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 服务器唯一id，每台服务器的id必须不同，如果配置其他从机，注意修改id</span></span>
<span class="line"><span style="color:#A6ACCD;">server-id</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">3</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 中继日志名，默认xxxxxxxxxxxx-relay-bin</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#relay-log=relay-bin</span></span></code></pre></div><p>重启MySQL容器</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">restart</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">atguigu-mysql-slave1</span></span>
<span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">restart</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">atguigu-mysql-slave2</span></span></code></pre></div><p><strong>step3：使用命令行登录MySQL从服务器：</strong></p><p>登录第一台服务器</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#进入容器：</span></span>
<span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">exec</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-it</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">atguigu-mysql-slave1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">env</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">LANG=C.UTF-8</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#进入容器内的mysql命令行</span></span>
<span class="line"><span style="color:#FFCB6B;">mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uroot</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#修改默认密码校验方式</span></span>
<span class="line"><span style="color:#FFCB6B;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">USER</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">IDENTIFIED</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">WITH</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">mysql_native_password</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">BY</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span></code></pre></div><p>登录第二台服务器</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#进入容器：</span></span>
<span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">exec</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-it</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">atguigu-mysql-slave2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">env</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">LANG=C.UTF-8</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#进入容器内的mysql命令行</span></span>
<span class="line"><span style="color:#FFCB6B;">mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uroot</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#修改默认密码校验方式</span></span>
<span class="line"><span style="color:#FFCB6B;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">USER</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">IDENTIFIED</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">WITH</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">mysql_native_password</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">BY</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span></code></pre></div><p><strong>step4：在从机上配置主从关系：</strong></p><p>在<strong>所有从机</strong>上执行以下SQL操作</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">CHANGE </span><span style="color:#F78C6C;">MASTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TO</span><span style="color:#A6ACCD;"> MASTER_HOST</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">192.168.22.150</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, </span></span>
<span class="line"><span style="color:#A6ACCD;">MASTER_USER</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">atguigu_slave</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,MASTER_PASSWORD</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">, MASTER_PORT</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">3307</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">MASTER_LOG_FILE</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">binlog.000003</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,MASTER_LOG_POS</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1357</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="_3、启动主从同步" tabindex="-1">3、启动主从同步 <a class="header-anchor" href="#_3、启动主从同步" aria-label="Permalink to &quot;3、启动主从同步&quot;">​</a></h3><p>启动从机的复制功能，执行SQL：</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">START</span><span style="color:#A6ACCD;"> SLAVE;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 查看状态（不需要分号）</span></span>
<span class="line"><span style="color:#A6ACCD;">SHOW SLAVE </span><span style="color:#F78C6C;">STATUS</span><span style="color:#A6ACCD;">\G</span></span></code></pre></div><p>**两个关键进程：**下面两个参数都是Yes，则说明主从配置成功！</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251707023.png" alt="image-20221125170701946" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251707653.png" alt="image-20221125170730540" style="zoom:80%;"><h3 id="_4、实现主从同步" tabindex="-1">4、实现主从同步 <a class="header-anchor" href="#_4、实现主从同步" aria-label="Permalink to &quot;4、实现主从同步&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251708793.png" alt="image-20221125170801714" style="zoom:80%;"><p>在主机中执行以下SQL，在从机中查看数据库、表和数据是否已经被同步</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">DATABASE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">db_user</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">USE</span><span style="color:#A6ACCD;"> db_user;</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">t_user</span><span style="color:#A6ACCD;"> (</span></span>
<span class="line"><span style="color:#A6ACCD;"> id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;"> AUTO_INCREMENT,</span></span>
<span class="line"><span style="color:#A6ACCD;"> uname </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;"> (id)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> t_user(uname) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">zhang3</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">INSERT INTO</span><span style="color:#A6ACCD;"> t_user(uname) </span><span style="color:#F78C6C;">VALUES</span><span style="color:#A6ACCD;">(@@hostname);</span></span></code></pre></div><p>从数据库1：发现数据已经成功同步</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251710005.png" alt="image-20221125171027868" style="zoom:80%;"><p>从数据库2：发现数据已经成功同步</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251710247.png" alt="image-20221125171042125" style="zoom:80%;"><h3 id="_5、停止和重置" tabindex="-1">5、停止和重置 <a class="header-anchor" href="#_5、停止和重置" aria-label="Permalink to &quot;5、停止和重置&quot;">​</a></h3><p>需要的时候，可以使用如下SQL语句</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 在从机上执行。功能说明：停止I/O 线程和SQL线程的操作。</span></span>
<span class="line"><span style="color:#F78C6C;">stop</span><span style="color:#A6ACCD;"> slave; </span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 在从机上执行。功能说明：用于删除SLAVE数据库的relaylog日志文件，并重新启用新的relaylog文件。</span></span>
<span class="line"><span style="color:#F78C6C;">reset</span><span style="color:#A6ACCD;"> slave;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 在主机上执行。功能说明：删除所有的binglog日志文件，并将日志索引文件清空，重新开始所有新的日志文件。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 用于第一次进行搭建主从库时，进行主库binlog初始化工作；</span></span>
<span class="line"><span style="color:#F78C6C;">reset</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">master</span><span style="color:#A6ACCD;">;</span></span></code></pre></div><h3 id="_6、常见问题" tabindex="-1">6、常见问题 <a class="header-anchor" href="#_6、常见问题" aria-label="Permalink to &quot;6、常见问题&quot;">​</a></h3><h4 id="问题1" tabindex="-1">问题1 <a class="header-anchor" href="#问题1" aria-label="Permalink to &quot;问题1&quot;">​</a></h4><p>启动主从同步后，常见错误是<code>Slave_IO_Running： No 或者 Connecting</code> 的情况，此时查看下方的 <code>Last_IO_ERROR</code>错误日志，根据日志中显示的错误信息在网上搜索解决方案即可</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804989.png" alt="img"></p><p><strong>典型的错误例如：</strong><code>Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: &#39;Client requested master to start replication from position &gt; file size&#39;</code></p><p><strong>解决方案</strong></p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 在从机停止slave</span></span>
<span class="line"><span style="color:#A6ACCD;">SLAVE </span><span style="color:#F78C6C;">STOP</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 在主机查看mater状态</span></span>
<span class="line"><span style="color:#A6ACCD;">SHOW </span><span style="color:#F78C6C;">MASTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">STATUS</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 在主机刷新日志</span></span>
<span class="line"><span style="color:#A6ACCD;">FLUSH LOGS;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 再次在主机查看mater状态（会发现File和Position发生了变化）</span></span>
<span class="line"><span style="color:#A6ACCD;">SHOW </span><span style="color:#F78C6C;">MASTER</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">STATUS</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">-- 修改从机连接主机的SQL，并重新连接即可</span></span></code></pre></div><h4 id="问题2" tabindex="-1">问题2 <a class="header-anchor" href="#问题2" aria-label="Permalink to &quot;问题2&quot;">​</a></h4><p>启动docker容器后提示 <code>WARNING: IPv4 forwarding is disabled. Networking will not work.</code></p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804005.png" alt="img"></p><p>此错误，虽然不影响主从同步的搭建，但是如果想从远程客户端通过以下方式连接docker中的MySQL则没法连接</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">C:\Users\administrator&gt;mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-h</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">192.168</span><span style="color:#C3E88D;">.100.201</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-P</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3306</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-u</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span></span></code></pre></div><p><strong>解决方案：</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#修改配置文件：</span></span>
<span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/lib/sysctl.d/00-system.conf</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#追加</span></span>
<span class="line"><span style="color:#FFCB6B;">net.ipv4.ip_forward</span><span style="color:#A6ACCD;">=1</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#接着重启网络</span></span>
<span class="line"><span style="color:#FFCB6B;">systemctl</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">restart</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">network</span></span></code></pre></div><h1 id="shardingsphere-jdbc读写分离" tabindex="-1">ShardingSphere-JDBC读写分离 <a class="header-anchor" href="#shardingsphere-jdbc读写分离" aria-label="Permalink to &quot;ShardingSphere-JDBC读写分离&quot;">​</a></h1><h2 id="_1、创建springboot程序" tabindex="-1">1、创建SpringBoot程序 <a class="header-anchor" href="#_1、创建springboot程序" aria-label="Permalink to &quot;1、创建SpringBoot程序&quot;">​</a></h2><h3 id="_1、创建项目" tabindex="-1">1、创建项目 <a class="header-anchor" href="#_1、创建项目" aria-label="Permalink to &quot;1、创建项目&quot;">​</a></h3><blockquote><p>项目类型：Spring Initializr</p><p>SpringBoot脚手架：<a href="http://start.aliyun.com" target="_blank" rel="noreferrer">http://start.aliyun.com</a></p><p>项目名：sharding-jdbc-demo</p><p>SpringBoot版本：2.4.1</p></blockquote><h3 id="_2、添加依赖" tabindex="-1">2、添加依赖 <a class="header-anchor" href="#_2、添加依赖" aria-label="Permalink to &quot;2、添加依赖&quot;">​</a></h3><div class="language-xml"><button title="Copy Code" class="copy"></button><span class="lang">xml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependencies</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">org.springframework.boot</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">spring-boot-starter-web</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">org.apache.shardingsphere</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">shardingsphere-jdbc-core-spring-boot-starter</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">version</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">5.1.1</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">version</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">mysql-connector-java</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">scope</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">runtime</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">scope</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">com.baomidou</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">mybatis-plus-boot-starter</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">version</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">3.3.1</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">version</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">org.projectlombok</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">lombok</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">optional</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">true</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">optional</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span></span>
<span class="line"><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependencies</span><span style="color:#89DDFF;">&gt;</span></span></code></pre></div><h3 id="_3、创建实体类" tabindex="-1">3、创建实体类 <a class="header-anchor" href="#_3、创建实体类" aria-label="Permalink to &quot;3、创建实体类&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableName</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">t_user</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Data</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">User</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableId</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">type</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> IdType</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">AUTO</span><span style="color:#89DDFF;">)</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;">// 使用数据库的主键自增策略</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">String</span><span style="color:#A6ACCD;"> uname</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_4、创建mapper" tabindex="-1">4、创建Mapper <a class="header-anchor" href="#_4、创建mapper" aria-label="Permalink to &quot;4、创建Mapper&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Mapper</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">interface</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">UserMapper</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">extends</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">BaseMapper</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">User</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_5、配置读写分离" tabindex="-1">5、配置读写分离 <a class="header-anchor" href="#_5、配置读写分离" aria-label="Permalink to &quot;5、配置读写分离&quot;">​</a></h3><p>application.properties：</p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 应用名称</span></span>
<span class="line"><span style="color:#F07178;">spring.application.name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">sharingTest</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 应用服务 WEB 访问端口</span></span>
<span class="line"><span style="color:#F07178;">server.port</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">8080</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 开发环境设置</span></span>
<span class="line"><span style="color:#F07178;">spring.profiles.active</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">dev</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 内存模式，单机模式，集群模式，上线要使用集群模式</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.mode.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">Memory</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置真实数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.names</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">master,slave1,slave2</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 1 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.master.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.master.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.master.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.22.150:3307/db_user</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.master.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.master.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 2 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.slave1.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.slave1.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.slave1.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.22.150:3308/db_user</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.slave1.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.slave1.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 3 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.slave2.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.slave2.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.slave2.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.22.150:3309/db_user</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.slave2.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.slave2.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 读写分离类型，如: Static，Dynamic，myds是名字，自己起的</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">Static</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 写数据源名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.write-data-source-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">master</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 读数据源名称，多个从数据源用逗号分隔</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.read-data-source-names</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">slave1,slave2</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 负载均衡算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.load-balancer-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_round</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 负载均衡算法配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 负载均衡算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_round.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">ROUND_ROBIN</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_random.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">RANDOM</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">WEIGHT</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave1</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">1</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave2</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">2</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 打印SQL</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.props.sql-show</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">true</span></span></code></pre></div><h2 id="_2、测试" tabindex="-1">2、测试 <a class="header-anchor" href="#_2、测试" aria-label="Permalink to &quot;2、测试&quot;">​</a></h2><h3 id="_1、读写分离测试" tabindex="-1">1、读写分离测试 <a class="header-anchor" href="#_1、读写分离测试" aria-label="Permalink to &quot;1、读写分离测试&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">SpringBootTest</span></span>
<span class="line"><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">ReadwriteTest</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Autowired</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">UserMapper</span><span style="color:#A6ACCD;"> userMapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">// 写入数据的测试</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testInsert</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C792EA;">User</span><span style="color:#A6ACCD;"> user </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">User</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">        user</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUname</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">张三丰</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        userMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">user</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251745246.png" alt="image-20221125174524160" style="zoom:80%;"><h3 id="_2、事务测试" tabindex="-1">2、事务测试 <a class="header-anchor" href="#_2、事务测试" aria-label="Permalink to &quot;2、事务测试&quot;">​</a></h3><p>为了保证主从库间的事务一致性，避免跨服务的分布式事务，ShardingSphere-JDBC的主从模型中，事务中的数据读写均用主库</p><blockquote><ul><li>不添加@Transactional：insert对主库操作，select对从库操作</li><li>添加@Transactional：则insert和select均对主库操作</li><li>**注意：**在JUnit环境下的@Transactional注解，默认情况下就会对事务进行回滚（即使在没加注解@Rollback，也会对事务回滚）</li></ul></blockquote><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">// 事务测试</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Transactional</span><span style="color:#676E95;font-style:italic;">//开启事务</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testTrans</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">User</span><span style="color:#A6ACCD;"> user </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">User</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">    user</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUname</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">铁锤</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    userMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">user</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">User</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> users </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> userMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">selectList</span><span style="color:#89DDFF;">(null);</span></span>
<span class="line"><span style="color:#A6ACCD;">    System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">users</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251935746.png" alt="image-20221125193529652" style="zoom:80%;"><h3 id="_3、负载均衡测试" tabindex="-1">3、负载均衡测试 <a class="header-anchor" href="#_3、负载均衡测试" aria-label="Permalink to &quot;3、负载均衡测试&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">// 读数据测试</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testSelectAll</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">User</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> users </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> userMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">selectList</span><span style="color:#89DDFF;">(null);</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">User</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> users1 </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> userMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">selectList</span><span style="color:#89DDFF;">(null);</span><span style="color:#676E95;font-style:italic;">//执行第二次测试负载均衡</span></span>
<span class="line"><span style="color:#A6ACCD;">    users</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">forEach</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;font-style:italic;">::</span><span style="color:#A6ACCD;">println</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251938891.png" alt="image-20221125193825804" style="zoom:80%;"><p>也可以在web请求中测试负载均衡</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">package</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">com</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">atguigu</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">shardingjdbcdemo</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">controller</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">RestController</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">RequestMapping</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/user</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">UserController</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Autowired</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">UserMapper</span><span style="color:#A6ACCD;"> userMapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">    /**</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">     * 测试负载均衡策略</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">     */</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">GetMapping</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">selectAll</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">User</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">selectAll</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">       </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#A6ACCD;"> userMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">selectList</span><span style="color:#89DDFF;">(null);</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h2 id="_3、负载均衡算法" tabindex="-1">3、负载均衡算法 <a class="header-anchor" href="#_3、负载均衡算法" aria-label="Permalink to &quot;3、负载均衡算法&quot;">​</a></h2><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 负载均衡算法名称(配置要选择的负载均衡算法)</span></span>
<span class="line"><span style="color:#A6ACCD;">spring.shardingsphere.rules.readwrite-splitting.data-sources.myds</span></span>
<span class="line"><span style="color:#A6ACCD;">.</span><span style="color:#F07178;">load-balancer-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_round</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 负载均衡算法类型</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 轮询</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_round.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">ROUND_ROBIN</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 随机</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_random.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">RANDOM</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 权重</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">WEIGHT</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave1</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">1</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave2</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">2</span></span></code></pre></div><h1 id="shardingsphere-jdbc垂直分片" tabindex="-1">ShardingSphere-JDBC垂直分片 <a class="header-anchor" href="#shardingsphere-jdbc垂直分片" aria-label="Permalink to &quot;ShardingSphere-JDBC垂直分片&quot;">​</a></h1><h2 id="_1、准备服务器" tabindex="-1">1、准备服务器 <a class="header-anchor" href="#_1、准备服务器" aria-label="Permalink to &quot;1、准备服务器&quot;">​</a></h2><p>服务器规划：使用<code>docker</code>方式创建如下容器</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211251956697.png" alt="image-20220807232456342"></p><ul><li><p>服务器：容器名<code>server-user</code>，端口<code>3301</code></p></li><li><p>服务器：容器名<code>server-order</code>，端口<code>3302</code></p></li></ul><h3 id="_1、创建server-user容器" tabindex="-1">1、创建server-user容器 <a class="header-anchor" href="#_1、创建server-user容器" aria-label="Permalink to &quot;1、创建server-user容器&quot;">​</a></h3><p><strong>step1：创建容器</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">run</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-p </span><span style="color:#F78C6C;">3301</span><span style="color:#C3E88D;">:3306</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/server/user/conf:/etc/mysql/conf.d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/server/user/data:/var/lib/mysql</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-e </span><span style="color:#C3E88D;">MYSQL_ROOT_PASSWORD=</span><span style="color:#F78C6C;">123456</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">--name </span><span style="color:#C3E88D;">server-user</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql:8.0.29</span></span></code></pre></div><p><strong>step2：登录MySQL服务器</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#进入容器：</span></span>
<span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">exec</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-it</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">server-user</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">env</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">LANG=C.UTF-8</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#进入容器内的mysql命令行</span></span>
<span class="line"><span style="color:#FFCB6B;">mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uroot</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#修改默认密码插件</span></span>
<span class="line"><span style="color:#FFCB6B;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">USER</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">IDENTIFIED</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">WITH</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">mysql_native_password</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">BY</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span></code></pre></div><p><strong>step3：创建数据库</strong></p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">DATABASE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">db_user</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">USE</span><span style="color:#A6ACCD;"> db_user;</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">t_user</span><span style="color:#A6ACCD;"> (</span></span>
<span class="line"><span style="color:#A6ACCD;"> id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;"> AUTO_INCREMENT,</span></span>
<span class="line"><span style="color:#A6ACCD;"> uname </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;"> (id)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="_2、创建server-order容器" tabindex="-1">2、创建server-order容器 <a class="header-anchor" href="#_2、创建server-order容器" aria-label="Permalink to &quot;2、创建server-order容器&quot;">​</a></h3><p><strong>step1：创建容器</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">run</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-p </span><span style="color:#F78C6C;">3302</span><span style="color:#C3E88D;">:3306</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/server/order/conf:/etc/mysql/conf.d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/server/order/data:/var/lib/mysql</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-e </span><span style="color:#C3E88D;">MYSQL_ROOT_PASSWORD=</span><span style="color:#F78C6C;">123456</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">--name </span><span style="color:#C3E88D;">server-order</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql:8.0.29</span></span></code></pre></div><p><strong>step2：登录MySQL服务器</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#进入容器：</span></span>
<span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">exec</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-it</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">server-order</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">env</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">LANG=C.UTF-8</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#进入容器内的mysql命令行</span></span>
<span class="line"><span style="color:#FFCB6B;">mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uroot</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#修改默认密码插件</span></span>
<span class="line"><span style="color:#FFCB6B;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">USER</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">IDENTIFIED</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">WITH</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">mysql_native_password</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">BY</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span></code></pre></div><p><strong>step3：创建数据库</strong></p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">DATABASE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">db_order</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">USE</span><span style="color:#A6ACCD;"> db_order;</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">t_order</span><span style="color:#A6ACCD;"> (</span></span>
<span class="line"><span style="color:#A6ACCD;">  id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;"> AUTO_INCREMENT,</span></span>
<span class="line"><span style="color:#A6ACCD;">  order_no </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  user_id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  amount </span><span style="color:#C792EA;">DECIMAL</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">(id) </span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span></code></pre></div><h2 id="_2、程序实现" tabindex="-1">2、程序实现 <a class="header-anchor" href="#_2、程序实现" aria-label="Permalink to &quot;2、程序实现&quot;">​</a></h2><h3 id="_1、创建实体类" tabindex="-1">1、创建实体类 <a class="header-anchor" href="#_1、创建实体类" aria-label="Permalink to &quot;1、创建实体类&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableName</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">t_order</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Data</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">Order</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableId</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">type</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> IdType</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">AUTO</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">String</span><span style="color:#A6ACCD;"> orderNo</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> userId</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">BigDecimal</span><span style="color:#A6ACCD;"> amount</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableName</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">t_user</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Data</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">User</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableId</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">type</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> IdType</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">AUTO</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">String</span><span style="color:#A6ACCD;"> uname</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_2、创建mapper" tabindex="-1">2、创建Mapper <a class="header-anchor" href="#_2、创建mapper" aria-label="Permalink to &quot;2、创建Mapper&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">import</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">com</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">baomidou</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">mybatisplus</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">core</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">mapper</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">BaseMapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F78C6C;">import</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">com</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">it</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">entity</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">User</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F78C6C;">import</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">org</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">apache</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">ibatis</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">annotations</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">Mapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Mapper</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">interface</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">OrderMapper</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">extends</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">BaseMapper</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Order</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">import</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">com</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">baomidou</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">mybatisplus</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">core</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">mapper</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">BaseMapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F78C6C;">import</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">com</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">it</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">entity</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">User</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F78C6C;">import</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">org</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">apache</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">ibatis</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">annotations</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">Mapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Mapper</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">interface</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">UserMapper</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">extends</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">BaseMapper</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">User</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_3、配置垂直分片" tabindex="-1">3、配置垂直分片 <a class="header-anchor" href="#_3、配置垂直分片" aria-label="Permalink to &quot;3、配置垂直分片&quot;">​</a></h3><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 应用名称</span></span>
<span class="line"><span style="color:#F07178;">spring.application.name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">sharingTest</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 应用服务 WEB 访问端口</span></span>
<span class="line"><span style="color:#F07178;">server.port</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">8080</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 环境设置</span></span>
<span class="line"><span style="color:#F07178;">spring.profiles.active</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">dev</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置真实数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.names</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-user,server-order</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 1 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-user.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-user.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-user.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.22.150:3301/db_user</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-user.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-user.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 2 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.22.150:3302/db_order</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 标准分片表配置（数据节点）</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># spring.shardingsphere.rules.sharding.tables.&lt;table-name&gt;.actual-data-nodes=值</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 值由数据源名 + 表名组成，以小数点分隔。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &lt;table-name&gt;：逻辑表名</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-user.t_user</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order.t_order</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 打印SQL</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.props.sql-show</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">true</span></span></code></pre></div><h2 id="_3、测试垂直分片" tabindex="-1">3、测试垂直分片 <a class="header-anchor" href="#_3、测试垂直分片" aria-label="Permalink to &quot;3、测试垂直分片&quot;">​</a></h2><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">SpringBootTest</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">ShardingTest</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Autowired</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">UserMapper</span><span style="color:#A6ACCD;"> userMapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Autowired</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">OrderMapper</span><span style="color:#A6ACCD;"> orderMapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">    /**</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">     * 垂直分片：插入数据测试</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">     */</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testInsertOrderAndUser</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">// 往User表插入数据</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C792EA;">User</span><span style="color:#A6ACCD;"> user </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">User</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">        user</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUname</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">强哥</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        userMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">user</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;">// 往Order表插入数据</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C792EA;">Order</span><span style="color:#A6ACCD;"> order </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">Order</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setOrderNo</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU001</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUserId</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">user</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getId</span><span style="color:#89DDFF;">());</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setAmount</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">BigDecimal</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">100</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#A6ACCD;">        orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">order</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">// 垂直分片：查询数据测试</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testSelectFromOrderAndUser</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C792EA;">User</span><span style="color:#A6ACCD;"> user </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> userMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">selectById</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">1L</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C792EA;">Order</span><span style="color:#A6ACCD;"> order </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">selectById</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">1L</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">user</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">order</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181102065.png" alt="image-20220918110239947" style="zoom:80%;"><h1 id="shardingsphere-jdbc水平分片" tabindex="-1">ShardingSphere-JDBC水平分片 <a class="header-anchor" href="#shardingsphere-jdbc水平分片" aria-label="Permalink to &quot;ShardingSphere-JDBC水平分片&quot;">​</a></h1><h2 id="_1、准备服务器-1" tabindex="-1">1、准备服务器 <a class="header-anchor" href="#_1、准备服务器-1" aria-label="Permalink to &quot;1、准备服务器&quot;">​</a></h2><p>服务器规划：使用<code>docker</code>方式创建如下容器</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211252010465.png" alt="image-20220808033239206"></p><ul><li><p>服务器：容器名<code>server-order0</code>，端口<code>3310</code></p></li><li><p>服务器：容器名<code>server-order1</code>，端口<code>3311</code></p></li></ul><h3 id="_1、创建server-order0容器" tabindex="-1">1、创建server-order0容器 <a class="header-anchor" href="#_1、创建server-order0容器" aria-label="Permalink to &quot;1、创建server-order0容器&quot;">​</a></h3><p><strong>step1：创建容器：</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">run</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-p </span><span style="color:#F78C6C;">3310</span><span style="color:#C3E88D;">:3306</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/server/order0/conf:/etc/mysql/conf.d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/server/order0/data:/var/lib/mysql</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-e </span><span style="color:#C3E88D;">MYSQL_ROOT_PASSWORD=</span><span style="color:#F78C6C;">123456</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">--name </span><span style="color:#C3E88D;">server-order0</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql:8.0.29</span></span></code></pre></div><p><strong>step2：登录MySQL服务器：</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#进入容器：</span></span>
<span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">exec</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-it</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">server-order0</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">env</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">LANG=C.UTF-8</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#进入容器内的mysql命令行</span></span>
<span class="line"><span style="color:#FFCB6B;">mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uroot</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#修改默认密码插件</span></span>
<span class="line"><span style="color:#FFCB6B;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">USER</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">IDENTIFIED</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">WITH</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">mysql_native_password</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">BY</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span></code></pre></div><p><strong>step3：创建数据库：</strong></p><p><code>注意：</code>水平分片的id需要在业务层实现，<code>不能依赖数据库的主键自增</code></p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">DATABASE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">db_order</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">USE</span><span style="color:#A6ACCD;"> db_order;</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">t_order0</span><span style="color:#A6ACCD;"> (</span></span>
<span class="line"><span style="color:#A6ACCD;">  id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  order_no </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  user_id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  amount </span><span style="color:#C792EA;">DECIMAL</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">(id) </span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">t_order1</span><span style="color:#A6ACCD;"> (</span></span>
<span class="line"><span style="color:#A6ACCD;">  id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  order_no </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  user_id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  amount </span><span style="color:#C792EA;">DECIMAL</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">(id) </span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="_2、创建server-order1容器" tabindex="-1">2、创建server-order1容器 <a class="header-anchor" href="#_2、创建server-order1容器" aria-label="Permalink to &quot;2、创建server-order1容器&quot;">​</a></h3><p><strong>step1：创建容器：</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">run</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-p </span><span style="color:#F78C6C;">3311</span><span style="color:#C3E88D;">:3306</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/server/order1/conf:/etc/mysql/conf.d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/server/order1/data:/var/lib/mysql</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-e </span><span style="color:#C3E88D;">MYSQL_ROOT_PASSWORD=</span><span style="color:#F78C6C;">123456</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">--name </span><span style="color:#C3E88D;">server-order1</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql:8.0.29</span></span></code></pre></div><p><strong>step2：登录MySQL服务器：</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#进入容器：</span></span>
<span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">exec</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-it</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">server-order1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">env</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">LANG=C.UTF-8</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#进入容器内的mysql命令行</span></span>
<span class="line"><span style="color:#FFCB6B;">mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uroot</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#修改默认密码插件</span></span>
<span class="line"><span style="color:#FFCB6B;">ALTER</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">USER</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">root</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">@</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">%</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">IDENTIFIED</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">WITH</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">mysql_native_password</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">BY</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span></code></pre></div><p>**step3：创建数据库：**和server-order0相同</p><p><code>注意：</code>水平分片的id需要在业务层实现，不能依赖数据库的主键自增</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">DATABASE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">db_order</span><span style="color:#A6ACCD;">;</span></span>
<span class="line"><span style="color:#F78C6C;">USE</span><span style="color:#A6ACCD;"> db_order;</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">t_order0</span><span style="color:#A6ACCD;"> (</span></span>
<span class="line"><span style="color:#A6ACCD;">  id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  order_no </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  user_id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  amount </span><span style="color:#C792EA;">DECIMAL</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">(id) </span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">t_order1</span><span style="color:#A6ACCD;"> (</span></span>
<span class="line"><span style="color:#A6ACCD;">  id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  order_no </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  user_id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">  amount </span><span style="color:#C792EA;">DECIMAL</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">(id) </span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span></code></pre></div><h2 id="_2、水平分片分析" tabindex="-1">2、水平分片分析 <a class="header-anchor" href="#_2、水平分片分析" aria-label="Permalink to &quot;2、水平分片分析&quot;">​</a></h2><h3 id="_1、基本配置" tabindex="-1">1、基本配置 <a class="header-anchor" href="#_1、基本配置" aria-label="Permalink to &quot;1、基本配置&quot;">​</a></h3><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#========================基本配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 应用名称</span></span>
<span class="line"><span style="color:#F07178;">spring.application.name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">sharging-jdbc-demo</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 开发环境设置</span></span>
<span class="line"><span style="color:#F07178;">spring.profiles.active</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">dev</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 内存模式</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.mode.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">Memory</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 打印SQL</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.props.sql-show</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">true</span></span></code></pre></div><h3 id="_2、数据源配置" tabindex="-1">2、数据源配置 <a class="header-anchor" href="#_2、数据源配置" aria-label="Permalink to &quot;2、数据源配置&quot;">​</a></h3><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#========================数据源配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置真实数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.names</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order0,server-order1</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 2 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.22.150:3310/db_order</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 3 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.22.150:3311/db_order</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order0.t_order0,server-order0.t_order1,server-order1.t_order0,server-order1.t_order1</span></span></code></pre></div><h3 id="_3、标椎分片表配置" tabindex="-1">3、标椎分片表配置 <a class="header-anchor" href="#_3、标椎分片表配置" aria-label="Permalink to &quot;3、标椎分片表配置&quot;">​</a></h3><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#========================标准分片表配置（数据节点配置）</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># spring.shardingsphere.rules.sharding.tables.&lt;table-name&gt;.actual-data-nodes=值</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 值由数据源名 + 表名组成，以小数点分隔。多个表以逗号分隔，支持 inline 表达式。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &lt;table-name&gt;：逻辑表名</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=server-user.t_user</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order0.t_order0,server-order0.t_order1,server-order1.t_order0,server-order1.t_order1</span></span></code></pre></div><p>修改Order实体类的主键策略：</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">//@TableId(type = IdType.AUTO)//依赖数据库的主键自增策略</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableId</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">type</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> IdType</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">ASSIGN_ID</span><span style="color:#89DDFF;">)</span><span style="color:#676E95;font-style:italic;">//分布式id</span></span></code></pre></div><p>测试：保留上面配置中的一个分片表节点分别进行测试，检查每个分片节点是否可用</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">// 水平分片：插入数据测试</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testInsertOrder</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">Order</span><span style="color:#A6ACCD;"> order </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">Order</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">    order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setOrderNo</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU001</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUserId</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">1L</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setAmount</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">BigDecimal</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">100</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#A6ACCD;">    orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">order</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.12.30/202211252034792.png" alt="image-20221125203431688" style="zoom:80%;"><h3 id="_4、行表达式-优化" tabindex="-1">4、行表达式(优化) <a class="header-anchor" href="#_4、行表达式-优化" aria-label="Permalink to &quot;4、行表达式(优化)&quot;">​</a></h3><p>优化上一步的分片表配置</p><p><a href="https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/inline-expression/" target="_blank" rel="noreferrer">https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/inline-expression/</a></p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#========================标准分片表配置（数据节点配置）</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># spring.shardingsphere.rules.sharding.tables.&lt;table-name&gt;.actual-data-nodes=值</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 值由数据源名 + 表名组成，以小数点分隔。多个表以逗号分隔，支持 inline 表达式。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &lt;table-name&gt;：逻辑表名</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=server-user.t_user</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># order$-&gt;{0..1}连续，order$-&gt;[0..1]枚举，都是表示order0和order1表</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order$-&gt;{0..1}.t_order$-&gt;{0..1}</span></span></code></pre></div><h3 id="_5、分片算法配置" tabindex="-1">5、分片算法配置 <a class="header-anchor" href="#_5、分片算法配置" aria-label="Permalink to &quot;5、分片算法配置&quot;">​</a></h3><p><strong>水平分库</strong></p><p>分片规则：order表中<code>user_id</code>为偶数时，数据插入<code>server-order0服务器</code>，<code>user_id</code>为奇数时，数据插入<code>server-order1服务器</code>。这样分片的好处是，同一个用户的订单数据，一定会被插入到同一台服务器上，查询一个用户的订单时效率较高。</p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分库策略</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">user_id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_inline_userid</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分片算法配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 行表达式分片算法</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_inline_userid.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">INLINE</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法属性配置</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_inline_userid.props.algorithm-expression</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order$-&gt;{user_id % 2}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 取模分片算法</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_mod.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">MOD</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法属性配置</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_mod.props.sharding-count</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">2</span></span></code></pre></div><p>为了方便测试，先设置只在 <code>t_order0</code>表上进行测试</p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">xxx.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order$-&gt;{0..1}.t_order0</span></span></code></pre></div><p>测试：可以分别测试行表达式分片算法和取模分片算法</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">// 水平分片：分库插入数据测试</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testInsertOrderDatabaseStrategy</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">long</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">4</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">++)</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C792EA;">Order</span><span style="color:#A6ACCD;"> order </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">Order</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setOrderNo</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU001</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUserId</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">i </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setAmount</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">BigDecimal</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">100</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#A6ACCD;">        orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">order</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p><strong>水平分表</strong></p><p>分片规则：order表中<code>order_no的哈希值为偶数时</code>，数据插入对应服务器的<code>t_order0表</code>，<code>order_no的哈希值为奇数时</code>，数据插入对应服务器的<code>t_order1表</code>。因为order_no是字符串形式，因此不能直接取模。</p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分表策略</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">order_no</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_hash_mod</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分片算法配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 哈希取模分片算法</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_hash_mod.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">HASH_MOD</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法属性配置</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_hash_mod.props.sharding-count</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">2</span></span></code></pre></div><p>测试前不要忘记将如下节点改回原来的状态</p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">xxx.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order$-&gt;{0..1}.t_order$-&gt;{0..1}</span></span></code></pre></div><h3 id="_6、分布式序列算法" tabindex="-1">6、分布式序列算法 <a class="header-anchor" href="#_6、分布式序列算法" aria-label="Permalink to &quot;6、分布式序列算法&quot;">​</a></h3><p><strong>雪花算法：</strong></p><p><a href="https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/key-generator/" target="_blank" rel="noreferrer">https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/key-generator/</a></p><p>水平分片需要关注全局序列，因为不能简单的使用基于数据库的主键自增。</p><p>这里有两种方案：一种是基于MyBatisPlus的id策略；一种是ShardingSphere-JDBC的全局序列配置。</p><p><code>基于MyBatisPlus的id策略：</code>将Order类的id设置成如下形式</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableId</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">type</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> IdType</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">ASSIGN_ID</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">;</span></span></code></pre></div><p><code>基于ShardingSphere-JDBC的全局序列配置</code>：和前面的MyBatisPlus的策略二选一</p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分布式序列策略配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_snowflake</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.key-generators.alg_snowflake.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">SNOWFLAKE</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法属性配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#spring.shardingsphere.rules.sharding.key-generators.alg_snowflake.props.xxx=</span></span></code></pre></div><p>此时，需要将实体类中的id策略修改成以下形式：</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">//当配置了shardingsphere-jdbc的分布式序列时，自动使用shardingsphere-jdbc的分布式序列</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">//当没有配置shardingsphere-jdbc的分布式序列时，自动依赖数据库的主键自增策略</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableId</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">type</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> IdType</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">AUTO</span><span style="color:#89DDFF;">)</span></span></code></pre></div><h2 id="_3、水平分片完整实现⭐" tabindex="-1">3、水平分片完整实现⭐ <a class="header-anchor" href="#_3、水平分片完整实现⭐" aria-label="Permalink to &quot;3、水平分片完整实现⭐&quot;">​</a></h2><h3 id="_1、order" tabindex="-1">1、Order <a class="header-anchor" href="#_1、order" aria-label="Permalink to &quot;1、Order&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableName</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">t_order</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Data</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">Order</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">//@TableId(type = IdType.AUTO)//依赖数据库的主键自增策略</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableId</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">type</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> IdType</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">ASSIGN_ID</span><span style="color:#89DDFF;">)</span><span style="color:#676E95;font-style:italic;">//分布式id</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">String</span><span style="color:#A6ACCD;"> orderNo</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> userId</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">BigDecimal</span><span style="color:#A6ACCD;"> amount</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_2、ordermapper" tabindex="-1">2、OrderMapper <a class="header-anchor" href="#_2、ordermapper" aria-label="Permalink to &quot;2、OrderMapper&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">import</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">com</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">baomidou</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">mybatisplus</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">core</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">mapper</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">BaseMapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F78C6C;">import</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">com</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">it</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">entity</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">Order</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F78C6C;">import</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">org</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">apache</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">ibatis</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">annotations</span><span style="color:#89DDFF;">.</span><span style="color:#C792EA;">Mapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Mapper</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">interface</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">OrderMapper</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">extends</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">BaseMapper</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Order</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_3、application-properties⭐" tabindex="-1">3、application.properties⭐ <a class="header-anchor" href="#_3、application-properties⭐" aria-label="Permalink to &quot;3、application.properties⭐&quot;">​</a></h3><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 内存模式</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.mode.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">Memory</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 打印SQl</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.props.sql-show</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">true</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#========================数据源配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置真实数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.names</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order0,server-order1</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">## 配置第 1 个数据源</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#spring.shardingsphere.datasource.server-user.type=com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#spring.shardingsphere.datasource.server-user.driver-class-name=com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#spring.shardingsphere.datasource.server-user.jdbc-url=jdbc:mysql://192.168.0.155:3307/db_user</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#spring.shardingsphere.datasource.server-user.username=root</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#spring.shardingsphere.datasource.server-user.password=123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 2 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.0.155:3307/db_order</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 3 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.22.145:3306/db_order</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#========================标准分片表配置（数据节点配置）</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># spring.shardingsphere.rules.sharding.tables.&lt;table-name&gt;.actual-data-nodes=值</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 值由数据源名 + 表名组成，以小数点分隔。多个表以逗号分隔，支持 inline 表达式。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &lt;table-name&gt;：逻辑表名</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=server-user.t_user</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># order$-&gt;{0..1}连续，order$-&gt;[0..1]枚举，都是表示order0和order1表</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order$-&gt;{0..1}.t_order$-&gt;{0..1}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分库策略</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">user_id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_inline_userid</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分片算法配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 行表达式分片算法</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_inline_userid.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">INLINE</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法属性配置</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_inline_userid.props.algorithm-expression</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order$-&gt;{user_id % 2}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 取模分片算法</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_mod.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">MOD</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法属性配置</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_mod.props.sharding-count</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">2</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分表策略</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">order_no</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_hash_mod</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分片算法配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 哈希取模分片算法</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_hash_mod.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">HASH_MOD</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法属性配置</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_hash_mod.props.sharding-count</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">2</span></span></code></pre></div><h3 id="_4、测试插入" tabindex="-1">4、测试插入 <a class="header-anchor" href="#_4、测试插入" aria-label="Permalink to &quot;4、测试插入&quot;">​</a></h3><blockquote><p>可以发现，数据被分到4张表中了</p></blockquote><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testInsertOrderTableStrategy</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">long</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">++)</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C792EA;">Order</span><span style="color:#A6ACCD;"> order </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">Order</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setOrderNo</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUserId</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">1L</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setAmount</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">BigDecimal</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">100</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#A6ACCD;">        orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">order</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">long</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">9</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">++)</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C792EA;">Order</span><span style="color:#A6ACCD;"> order </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">Order</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setOrderNo</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUserId</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">2L</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setAmount</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">BigDecimal</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">100</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#A6ACCD;">        orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">order</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">/**</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">  * 测试哈希取模</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">*/</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testHash</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">//注意hash取模的结果是整个字符串hash后再取模，和数值后缀是奇数还是偶数无关</span></span>
<span class="line"><span style="color:#A6ACCD;">    System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU001</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">hashCode</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">%</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">println</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU0011</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">hashCode</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">%</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_5、测试查询" tabindex="-1">5、测试查询 <a class="header-anchor" href="#_5、测试查询" aria-label="Permalink to &quot;5、测试查询&quot;">​</a></h3><blockquote><p>可以查询到4张表的所有数据</p></blockquote><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">/**</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"> * 水平分片：查询所有记录</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"> * 查询了两个数据源，每个数据源中使用UNION ALL连接两个表</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"> */</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testShardingSelectAll</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Order</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> orders </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">selectList</span><span style="color:#89DDFF;">(null);</span></span>
<span class="line"><span style="color:#A6ACCD;">    orders</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">forEach</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;font-style:italic;">::</span><span style="color:#A6ACCD;">println</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">/**</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"> * 水平分片：根据user_id查询记录</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"> * 查询了一个数据源，每个数据源中使用UNION ALL连接两个表</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"> */</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testShardingSelectByUserId</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">QueryWrapper</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Order</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> orderQueryWrapper </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">QueryWrapper</span><span style="color:#89DDFF;">&lt;&gt;();</span></span>
<span class="line"><span style="color:#A6ACCD;">    orderQueryWrapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">eq</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">user_id</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1L</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Order</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> orders </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">selectList</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">orderQueryWrapper</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    orders</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">forEach</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;font-style:italic;">::</span><span style="color:#A6ACCD;">println</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181142139.png" alt="image-20220918114233974" style="zoom:80%;"><h2 id="_4、多表关联插入" tabindex="-1">4、多表关联插入 <a class="header-anchor" href="#_4、多表关联插入" aria-label="Permalink to &quot;4、多表关联插入&quot;">​</a></h2><h3 id="_1、创建关联表" tabindex="-1">1、创建关联表 <a class="header-anchor" href="#_1、创建关联表" aria-label="Permalink to &quot;1、创建关联表&quot;">​</a></h3><p>在<code>server-order0、server-order1</code>服务器中分别创建两张订单详情表<code>t_order_item0、t_order_item1</code></p><p>我们希望<code>同一个用户的订单表和订单详情表中的数据都在同一个数据源中，避免跨库关联</code>，因此这两张表我们使用相同的分片策略。</p><p>那么在<code>t_order_item</code>中我们也需要创建<code>order_no</code>和<code>user_id</code>这两个分片键</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">t_order_item0</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    order_no </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">    user_id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    price </span><span style="color:#C792EA;">DECIMAL</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">count</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">INT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">(id)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">t_order_item1</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    order_no </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">    user_id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    price </span><span style="color:#C792EA;">DECIMAL</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;">,</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">`</span><span style="color:#C3E88D;">count</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">INT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">(id)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="_2、创建实体类" tabindex="-1">2、创建实体类 <a class="header-anchor" href="#_2、创建实体类" aria-label="Permalink to &quot;2、创建实体类&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableName</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">t_order_item</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Data</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">OrderItem</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">//当配置了shardingsphere-jdbc的分布式序列时，自动使用shardingsphere-jdbc的分布式序列</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableId</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">type</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> IdType</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">AUTO</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">String</span><span style="color:#A6ACCD;"> orderNo</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> userId</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">BigDecimal</span><span style="color:#A6ACCD;"> price</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Integer</span><span style="color:#A6ACCD;"> count</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_3、创建mapper" tabindex="-1">3、创建Mapper <a class="header-anchor" href="#_3、创建mapper" aria-label="Permalink to &quot;3、创建Mapper&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Mapper</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">interface</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">OrderItemMapper</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">extends</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">BaseMapper</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">OrderItem</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_4、配置关联表" tabindex="-1">4、配置关联表 <a class="header-anchor" href="#_4、配置关联表" aria-label="Permalink to &quot;4、配置关联表&quot;">​</a></h3><p>t_order_item的分片表、分片策略、分布式序列策略和t_order一致</p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#------------------------标准分片表配置（数据节点配置）</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order$-&gt;{0..1}.t_order_item$-&gt;{0..1}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分库策略</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.database-strategy.standard.sharding-column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">user_id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.database-strategy.standard.sharding-algorithm-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_mod</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分表策略</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">order_no</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-algorithm-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_hash_mod</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分布式序列策略配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_snowflake</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.key-generators.alg_snowflake.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">SNOWFLAKE</span></span></code></pre></div><h3 id="_5、测试插入数据" tabindex="-1">5、测试插入数据 <a class="header-anchor" href="#_5、测试插入数据" aria-label="Permalink to &quot;5、测试插入数据&quot;">​</a></h3><p>同一个用户的订单表和订单详情表中的数据都在同一个数据源中，避免跨库关联</p><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">/**</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">     * 测试关联表插入</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">     */</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testInsertOrderAndOrderItem</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">long</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">++)</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C792EA;">Order</span><span style="color:#A6ACCD;"> order </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">Order</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setOrderNo</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUserId</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">1L</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">order</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">long</span><span style="color:#A6ACCD;"> j </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> j </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> j</span><span style="color:#89DDFF;">++)</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#C792EA;">OrderItem</span><span style="color:#A6ACCD;"> orderItem </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">OrderItem</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">            orderItem</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setOrderNo</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">            orderItem</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUserId</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">1L</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">            orderItem</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setPrice</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">BigDecimal</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">10</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#A6ACCD;">            orderItem</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setCount</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">            orderItemMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">orderItem</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">long</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">7</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">++)</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C792EA;">Order</span><span style="color:#A6ACCD;"> order </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">Order</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setOrderNo</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        order</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUserId</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">2L</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">order</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#C792EA;">long</span><span style="color:#A6ACCD;"> j </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> j </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> j</span><span style="color:#89DDFF;">++)</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#C792EA;">OrderItem</span><span style="color:#A6ACCD;"> orderItem </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">OrderItem</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">            orderItem</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setOrderNo</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">ATGUIGU</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> i</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">            orderItem</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setUserId</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">2L</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">            orderItem</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setPrice</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">BigDecimal</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">));</span></span>
<span class="line"><span style="color:#A6ACCD;">            orderItem</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setCount</span><span style="color:#89DDFF;">(</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">            orderItemMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">orderItem</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181202585.png" alt="image-20220918120250465" style="zoom:80%;"><h2 id="_5、多表关联查询" tabindex="-1">5、多表关联查询 <a class="header-anchor" href="#_5、多表关联查询" aria-label="Permalink to &quot;5、多表关联查询&quot;">​</a></h2><blockquote><p>**需求：**查询每个订单的订单号和总订单金额</p></blockquote><h3 id="_1、创建vo对象" tabindex="-1">1、创建VO对象 <a class="header-anchor" href="#_1、创建vo对象" aria-label="Permalink to &quot;1、创建VO对象&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Data</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">OrderVo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">String</span><span style="color:#A6ACCD;"> orderNo</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">BigDecimal</span><span style="color:#A6ACCD;"> amount</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_2、添加mapper方法" tabindex="-1">2、添加Mapper方法 <a class="header-anchor" href="#_2、添加mapper方法" aria-label="Permalink to &quot;2、添加Mapper方法&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Mapper</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">interface</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">OrderMapper</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">extends</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">BaseMapper</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Order</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Select</span><span style="color:#89DDFF;">({</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">SELECT o.order_no, SUM(i.price * i.count) AS amount</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">FROM t_order o JOIN t_order_item i ON o.order_no = i.order_no</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">GROUP BY o.order_no</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">})</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">OrderVo</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">getOrderAmount</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_3、测试关联查询" tabindex="-1">3、测试关联查询 <a class="header-anchor" href="#_3、测试关联查询" aria-label="Permalink to &quot;3、测试关联查询&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">// 测试关联表查询</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testGetOrderAmount</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">OrderVo</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> orderAmountList </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> orderMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">getOrderAmount</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">    orderAmountList</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">forEach</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;font-style:italic;">::</span><span style="color:#A6ACCD;">println</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_4、配置绑定表" tabindex="-1">4、配置绑定表 <a class="header-anchor" href="#_4、配置绑定表" aria-label="Permalink to &quot;4、配置绑定表&quot;">​</a></h3><p>在原来水平分片配置的基础上添加如下配置：</p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#---绑定表，多个绑定表就数组0，1，2.。顺序写就行，报黄标不用管</span></span>
<span class="line"><span style="color:#A6ACCD;">spring.shardingsphere.rules.sharding.binding-tables[0]=t_order,t_order_item</span></span></code></pre></div><p>配置完绑定表后再次进行关联查询的测试：不是结果8个，结果正常，只是执行了8次真实表查询</p><ul><li><p>**如果不配置绑定表：测试的结果为8个SQL。**多表关联查询会出现笛卡尔积关联。</p></li><li><p><strong>如果配置绑定表：测试的结果为4个SQL。</strong> 多表关联查询不会出现笛卡尔积关联，关联查询效率将大大提升。</p></li></ul><p><code>绑定表：</code>指分片规则一致的一组分片表。 使用绑定表进行多表关联查询时，必须使用分片键进行关联，否则会出现笛卡尔积关联或跨库关联，从而影响查询效率。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181232926.png" alt="image-20220918123257654" style="zoom:80%;"><h3 id="_5、完整配置⭐⭐" tabindex="-1">5、完整配置⭐⭐ <a class="header-anchor" href="#_5、完整配置⭐⭐" aria-label="Permalink to &quot;5、完整配置⭐⭐&quot;">​</a></h3><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#------------------------基本配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 开发环境设置</span></span>
<span class="line"><span style="color:#F07178;">spring.profiles.active</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">dev</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 内存模式</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.mode.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">Memory</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 打印SQl</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.props.sql-show</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">true</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------数据源配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置真实数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.names</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order0,server-order1</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 2 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.22.145:3306/db_order</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order0.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 配置第 3 个数据源</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.zaxxer.hikari.HikariDataSource</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.driver-class-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">com.mysql.jdbc.Driver</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.jdbc-url</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">jdbc:mysql://192.168.0.155:3307/db_order</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.username</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">root</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.datasource.server-order1.password</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">123456</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------标准分片表配置（数据节点配置）</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 由数据源名 + 表名组成，以小数点分隔。多个表以逗号分隔，支持 inline 表达式。</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order$-&gt;{0..1}.t_order$-&gt;{0..1}</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order$-&gt;{0..1}.t_order_item$-&gt;{0..1}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分库策略，缺省表示使用默认分库策略，以下的分片策略只能选其一</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 用于单分片键的标准分片场景</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">user_id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_mod</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.database-strategy.standard.sharding-column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">user_id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.database-strategy.standard.sharding-algorithm-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_mod</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分表策略</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 用于单分片键的标准分片场景</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">order_no</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_hash_mod</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">order_no</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-algorithm-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_hash_mod</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分片算法配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 行表达式分片算法</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_inline_userid.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">INLINE</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法属性配置</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_inline_userid.props.algorithm-expression</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-order$-&gt;{user_id % 2}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 取模分片算法</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_mod.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">MOD</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法属性配置</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_mod.props.sharding-count</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">2</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 哈希取模分片算法</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_hash_mod.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">HASH_MOD</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分片算法属性配置</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.sharding-algorithms.alg_hash_mod.props.sharding-count</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">2</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分布式序列策略配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_snowflake</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列列名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.column</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">id</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法名称</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">alg_snowflake</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------分布式序列算法配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法类型</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.key-generators.alg_snowflake.type</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">SNOWFLAKE</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 分布式序列算法属性配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#spring.shardingsphere.rules.sharding.key-generators.alg_snowflake.props.xxx=</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#------------------------绑定表配置</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 绑定表规则列表</span></span>
<span class="line"><span style="color:#A6ACCD;">spring.shardingsphere.rules.sharding.binding-tables[0]=t_order,t_order_item</span></span></code></pre></div><h2 id="_6、广播表" tabindex="-1">6、广播表 <a class="header-anchor" href="#_6、广播表" aria-label="Permalink to &quot;6、广播表&quot;">​</a></h2><h3 id="_1、什么是广播表" tabindex="-1">1、什么是广播表 <a class="header-anchor" href="#_1、什么是广播表" aria-label="Permalink to &quot;1、什么是广播表&quot;">​</a></h3><blockquote><p>指所有的分片数据源中都存在的表，表结构及其数据在每个数据库中均完全一致。 适用于数据量不大且需要与海量数据的表进行关联查询的场景，例如：字典表。</p></blockquote><p>广播具有以下特性：</p><blockquote><p>（1）插入、更新操作会实时在所有节点上执行，保持各个分片的数据一致性</p><p>（2）查询操作，只从一个节点获取</p><p>（3）可以跟任何一个表进行 JOIN 操作</p></blockquote><h3 id="_2、创建广播表" tabindex="-1">2、创建广播表 <a class="header-anchor" href="#_2、创建广播表" aria-label="Permalink to &quot;2、创建广播表&quot;">​</a></h3><p>在server-order0、server-order1和server-user服务器中分别创建t_dict表，表不是固定的</p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">CREATE</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">TABLE</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">t_dict</span><span style="color:#A6ACCD;">(</span></span>
<span class="line"><span style="color:#A6ACCD;">    id </span><span style="color:#C792EA;">BIGINT</span><span style="color:#A6ACCD;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">    dict_type </span><span style="color:#C792EA;">VARCHAR</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;">),</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">PRIMARY KEY</span><span style="color:#A6ACCD;">(id)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span></code></pre></div><h3 id="_3、程序实现" tabindex="-1">3、程序实现 <a class="header-anchor" href="#_3、程序实现" aria-label="Permalink to &quot;3、程序实现&quot;">​</a></h3><h4 id="_4-3-1、创建实体类" tabindex="-1">4.3.1、创建实体类 <a class="header-anchor" href="#_4-3-1、创建实体类" aria-label="Permalink to &quot;4.3.1、创建实体类&quot;">​</a></h4><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableName</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">t_dict</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Data</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">Dict</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">//可以使用MyBatisPlus的雪花算法</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableId</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">type</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> IdType</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">ASSIGN_ID</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">String</span><span style="color:#A6ACCD;"> dictType</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h4 id="_4-3-2、创建mapper" tabindex="-1">4.3.2、创建Mapper <a class="header-anchor" href="#_4-3-2、创建mapper" aria-label="Permalink to &quot;4.3.2、创建Mapper&quot;">​</a></h4><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Mapper</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">interface</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">DictMapper</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">extends</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">BaseMapper</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Dict</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h4 id="_4-3-3、配置广播表" tabindex="-1">4.3.3、配置广播表 <a class="header-anchor" href="#_4-3-3、配置广播表" aria-label="Permalink to &quot;4.3.3、配置广播表&quot;">​</a></h4><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 数据节点可不配置，默认情况下，向所有数据源广播</span></span>
<span class="line"><span style="color:#F07178;">spring.shardingsphere.rules.sharding.tables.t_dict.actual-data-nodes</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">server-user.t_dict,server-order$-&gt;{0..1}.t_dict</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 广播表</span></span>
<span class="line"><span style="color:#A6ACCD;">spring.shardingsphere.rules.sharding.broadcast-tables[0]=t_dict</span></span></code></pre></div><h3 id="_4、测试广播表" tabindex="-1">4、测试广播表 <a class="header-anchor" href="#_4、测试广播表" aria-label="Permalink to &quot;4、测试广播表&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Resource</span></span>
<span class="line"><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">DictMapper</span><span style="color:#A6ACCD;"> dictMapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">/**</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"> * 广播表：每个服务器中的t_dict同时添加了新数据</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">*/</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testBroadcast</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">Dict</span><span style="color:#A6ACCD;"> dict </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">new</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">Dict</span><span style="color:#89DDFF;">();</span></span>
<span class="line"><span style="color:#A6ACCD;">    dict</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">setDictType</span><span style="color:#89DDFF;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">type1</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">    dictMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">insert</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">dict</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;">/**</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">  * 查询操作，只从一个节点获取数据</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">  * 随机负载均衡规则</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">*/</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testSelectBroadcast</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">Dict</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> dicts </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> dictMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">selectList</span><span style="color:#89DDFF;">(null);</span></span>
<span class="line"><span style="color:#A6ACCD;">    dicts</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">forEach</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;font-style:italic;">::</span><span style="color:#A6ACCD;">println</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181252493.png" alt="image-20220918125235379" style="zoom:80%;"><h1 id="shardingsphere-proxy" tabindex="-1">ShardingSphere-Proxy <a class="header-anchor" href="#shardingsphere-proxy" aria-label="Permalink to &quot;ShardingSphere-Proxy&quot;">​</a></h1><h2 id="_1、获取" tabindex="-1">1、获取 <a class="header-anchor" href="#_1、获取" aria-label="Permalink to &quot;1、获取&quot;">​</a></h2><p>目前 ShardingSphere-Proxy 提供了 3 种获取方式：</p><ul><li><a href="https://shardingsphere.apache.org/document/5.1.1/cn/user-manual/shardingsphere-proxy/startup/bin/" target="_blank" rel="noreferrer">二进制发布包</a></li><li><a href="https://shardingsphere.apache.org/document/5.1.1/cn/user-manual/shardingsphere-proxy/startup/docker/" target="_blank" rel="noreferrer">Docker</a></li><li><a href="https://shardingsphere.apache.org/document/5.1.1/cn/user-manual/shardingsphere-proxy/startup/helm/" target="_blank" rel="noreferrer">Helm</a></li></ul><h2 id="_2、使用二进制发布包安装⭐⭐" tabindex="-1">2、使用二进制发布包安装⭐⭐ <a class="header-anchor" href="#_2、使用二进制发布包安装⭐⭐" aria-label="Permalink to &quot;2、使用二进制发布包安装⭐⭐&quot;">​</a></h2><p>二进制包既可以Linux系统运行，又可以在windows系统运行</p><h3 id="step1-解压二进制包" tabindex="-1">step1：解压二进制包 <a class="header-anchor" href="#step1-解压二进制包" aria-label="Permalink to &quot;step1：解压二进制包&quot;">​</a></h3><p><code>apache-shardingsphere-5.1.1-shardingsphere-proxy-bin.tar.gz</code></p><p>windows：使用解压软件解压文件</p><p>Linux：将文件上传至/root目录，并解压</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">tar</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-zxvf</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">apache-shardingsphere-5.1.1-shardingsphere-proxy-bin.tar.gz</span></span>
<span class="line"><span style="color:#FFCB6B;">mv</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">apache-shardingsphere-5.1.1-shardingsphere-proxy-bin</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">shardingsphere-proxy</span></span></code></pre></div><h3 id="step2-mysql驱动" tabindex="-1">step2：MySQL驱动 <a class="header-anchor" href="#step2-mysql驱动" aria-label="Permalink to &quot;step2：MySQL驱动&quot;">​</a></h3><p>新建<code>ext-lib</code>目录，并把<code>mysql-connector-java-8.0.22.jar</code>上传至该目录</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172137339.png" alt="image-20220917213744263" style="zoom:80%;"><h3 id="step3-修改配置conf-server-yaml" tabindex="-1">step3：修改配置conf/server.yaml <a class="header-anchor" href="#step3-修改配置conf-server-yaml" aria-label="Permalink to &quot;step3：修改配置conf/server.yaml&quot;">​</a></h3><blockquote><p>只需要解开注释即可,服务器的核心配置</p></blockquote><div class="language-yaml"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">rules</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">!AUTHORITY</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">users</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;"># 在任何服务器上都能访问该服务器：用户名root，密码root</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root@%:root</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">provider</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;"># 当前用户拥有所有访问权限</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">type</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">ALL_PRIVILEGES_PERMITTED</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 打印SQL</span></span>
<span class="line"><span style="color:#F07178;">props</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">sql-show</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span></code></pre></div><p><strong>spte4：启动ShardingSphere-Proxy</strong></p><p>Linux 操作系统请运行 <code>bin/start.sh</code></p><p>Windows 操作系统请运行 <code>bin/start.bat</code></p><p>指定端口号和配置文件目录：<code>bin/start.bat ${proxy_port} ${proxy_conf_directory}</code></p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">bin</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">start</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">sh </span><span style="color:#F78C6C;">3321</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172135609.png" alt="image-20220917213542488" style="zoom:80%;"><h3 id="step5-远程连接shardingsphere-proxy" tabindex="-1">step5：远程连接ShardingSphere-Proxy <a class="header-anchor" href="#step5-远程连接shardingsphere-proxy" aria-label="Permalink to &quot;step5：远程连接ShardingSphere-Proxy&quot;">​</a></h3><blockquote><p>密码就是配置文件的：root</p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-h127.0.0.1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-P3321</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uroot</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172134669.png" alt="image-20220917213431527" style="zoom:80%;"><h3 id="step6-访问测试" tabindex="-1">step6：访问测试 <a class="header-anchor" href="#step6-访问测试" aria-label="Permalink to &quot;step6：访问测试&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">show databases;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172134906.png" alt="image-20220917213402762" style="zoom:80%;"><h2 id="_3、使用docker安装" tabindex="-1">3、使用Docker安装 <a class="header-anchor" href="#_3、使用docker安装" aria-label="Permalink to &quot;3、使用Docker安装&quot;">​</a></h2><p><strong>step1：启动Docker容器</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">run</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/server/proxy-a/conf:/opt/shardingsphere-proxy/conf</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-v </span><span style="color:#C3E88D;">/atguigu/server/proxy-a/ext-lib:/opt/shardingsphere-proxy/ext-lib</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-e </span><span style="color:#C3E88D;">ES_JAVA_OPTS=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">-Xmx256m -Xms256m -Xmn128m</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">-p </span><span style="color:#F78C6C;">3321</span><span style="color:#C3E88D;">:3307</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">--name </span><span style="color:#C3E88D;">server-proxy-a</span><span style="color:#A6ACCD;"> \</span></span>
<span class="line"><span style="color:#A6ACCD;">apache/shardingsphere-proxy:5.1.1</span></span></code></pre></div><p><strong>step2：上传MySQL驱动</strong></p><p>将MySQl驱动上传至<code>/atguigu/server/proxy-a/ext-lib</code>目录</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171950106.png" alt="image-20220917195053975" style="zoom:80%;"><p><strong>spte3：修改配置server.yaml</strong></p><blockquote><p>服务器的核心配置</p></blockquote><div class="language-yaml"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">rules</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">!AUTHORITY</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">users</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;"># 在任何服务器上都能访问该服务器：用户名root，密码root</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root@%:root</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">provider</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;"># 当前用户拥有所有访问权限</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">type</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">ALL_PRIVILEGES_PERMITTED</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 打印SQL</span></span>
<span class="line"><span style="color:#F07178;">props</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">sql-show</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#FF9CAC;">true</span></span></code></pre></div><p>将配置文件上传至<code>/atguigu/server/proxy-a/conf</code>目录</p><p><strong>spte4：重启容器</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">restart</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">server-proxy-a</span></span></code></pre></div><p><strong>step5：远程连接ShardingSphere-Proxy</strong></p><p>ShardingSphere-Proxy容器中默认情况下没有mysql命令行客户端的安装，因此需要远程访问</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">mysql</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-h192.168.100.201</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-P3321</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uroot</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span></span></code></pre></div><p><strong>step6：访问测试</strong></p><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">show databases;</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804459.png" alt="image-20220819152009158"></p><p><strong>常见问题：docker容器无法远程连接</strong></p><p>容器可以成功的创建并启动，但是无法远程连接。排除防火墙和网络等问题后，看看是不是因为容器内存不足导致。</p><p><code>原因：</code>容器可分配内存不足</p><p><code>查看办法：</code>进入容器后查看ShardingSphere-Proxy的日志，如有有<code>cannot allocate memory</code>，则说明容器内存不足</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">docker</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">exec</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-it</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">server-proxy-a</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">env</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">LANG=C.UTF-8</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/opt/shardingsphere-proxy/logs</span></span>
<span class="line"><span style="color:#FFCB6B;">tail</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">stdout.log</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209171804470.png" alt="image-20220819151154763"></p><p><code>解决方案：</code>创建容器的时候使用JVM参数</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">-e</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">ES_JAVA_OPTS=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">-Xmx256m -Xms256m -Xmn128m</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><h1 id="shardingsphere-proxy读写分离" tabindex="-1">ShardingSphere-Proxy读写分离 <a class="header-anchor" href="#shardingsphere-proxy读写分离" aria-label="Permalink to &quot;ShardingSphere-Proxy读写分离&quot;">​</a></h1><h2 id="配置演示" tabindex="-1">配置演示 <a class="header-anchor" href="#配置演示" aria-label="Permalink to &quot;配置演示&quot;">​</a></h2><h3 id="_1、修改配置文件" tabindex="-1">1、修改配置文件 <a class="header-anchor" href="#_1、修改配置文件" aria-label="Permalink to &quot;1、修改配置文件&quot;">​</a></h3><p><strong>修改配置config-readwrite-splitting.yaml</strong></p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/shardingsphere-proxy</span></span></code></pre></div><div class="language-yaml"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 名字任意</span></span>
<span class="line"><span style="color:#F07178;">schemaName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">readwrite_splitting_db</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F07178;">dataSources</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">write_ds</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">url</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">jdbc:mysql://192.168.22.145:3307/ph4?serverTimezone=UTC&amp;useSSL=false</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">username</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">password</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">123456</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">connectionTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">idleTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxLifetimeMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1800000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">50</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">minPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">read_ds_0</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">url</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">jdbc:mysql://192.168.22.145:3308/ph4?serverTimezone=UTC&amp;useSSL=false</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">username</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">password</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">123456</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">connectionTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">idleTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxLifetimeMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1800000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">50</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">minPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">read_ds_1</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">url</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">jdbc:mysql://192.168.22.145:3309/ph4?serverTimezone=UTC&amp;useSSL=false</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">username</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">password</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">123456</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">connectionTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">idleTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxLifetimeMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1800000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">50</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">minPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F07178;">rules</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">!READWRITE_SPLITTING</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">dataSources</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">readwrite_ds</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">type</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Static</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">props</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">write-data-source-name</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">write_ds</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">read-data-source-names</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">read_ds_0,read_ds_1</span></span></code></pre></div><p>将配置文件上传至<code>/atguigu/server/proxy-a/conf</code>目录</p><h3 id="_2、重启服务" tabindex="-1">2、重启服务 <a class="header-anchor" href="#_2、重启服务" aria-label="Permalink to &quot;2、重启服务&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">bin/stop.sh</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3321</span></span>
<span class="line"><span style="color:#FFCB6B;">bin/start.sh</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3321</span></span></code></pre></div><h3 id="_3、访问测试" tabindex="-1">3、访问测试 <a class="header-anchor" href="#_3、访问测试" aria-label="Permalink to &quot;3、访问测试&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mysql </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">h127.0.</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">P3321 </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">uroot </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p</span></span>
<span class="line"><span style="color:#A6ACCD;">show databases;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172303712.png" alt="image-20220917230327619" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> readwrite_splitting_db;</span></span>
<span class="line"><span style="color:#A6ACCD;">show tables;</span></span>
<span class="line"><span style="color:#A6ACCD;"># 多次执行，根据下面日志可以发现是轮询从库执行的</span></span>
<span class="line"><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> author;</span></span>
<span class="line"><span style="color:#A6ACCD;"># 插入测试</span></span>
<span class="line"><span style="color:#F78C6C;">insert into</span><span style="color:#A6ACCD;"> author(id,</span><span style="color:#F78C6C;">name</span><span style="color:#A6ACCD;">,country) </span><span style="color:#F78C6C;">values</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">null</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">wangrui</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">,</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">JPA</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">);</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172304681.png" alt="image-20220917230432536" style="zoom:80%;"><h3 id="_4、实时查看日志" tabindex="-1">4、实时查看日志 <a class="header-anchor" href="#_4、实时查看日志" aria-label="Permalink to &quot;4、实时查看日志&quot;">​</a></h3><p>可以通过这种方式查看服务器中输出的SQL语句</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">tail</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/shardingsphere-proxy/logs/stdout.log</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172305310.png" alt="image-20220917230549188" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172308232.png" alt="image-20220917230827088" style="zoom:80%;"><h2 id="springboot访问proxy⭐" tabindex="-1">SpringBoot访问Proxy⭐ <a class="header-anchor" href="#springboot访问proxy⭐" aria-label="Permalink to &quot;SpringBoot访问Proxy⭐&quot;">​</a></h2><h3 id="_1、添加依赖" tabindex="-1">1、添加依赖 <a class="header-anchor" href="#_1、添加依赖" aria-label="Permalink to &quot;1、添加依赖&quot;">​</a></h3><div class="language-xml"><button title="Copy Code" class="copy"></button><span class="lang">xml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependencies</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">org.springframework.boot</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">spring-boot-starter-web</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">mysql-connector-java</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">version</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">8.0.22</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">version</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">com.baomidou</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">mybatis-plus-boot-starter</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">version</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">3.3.1</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">version</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">org.projectlombok</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">groupId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">lombok</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">artifactId</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">optional</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">true</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">optional</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependency</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">dependencies</span><span style="color:#89DDFF;">&gt;</span></span></code></pre></div><h3 id="_2、创建实体类-1" tabindex="-1">2、创建实体类 <a class="header-anchor" href="#_2、创建实体类-1" aria-label="Permalink to &quot;2、创建实体类&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Data</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Accessors</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">chain</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">true)</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;">// 方便链式设置对象</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">class</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">author</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;">// 遵循数据库自增逻辑插入ID，不再自己插入一串字符了</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">TableId</span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">type</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> IdType</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">AUTO</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">Long</span><span style="color:#A6ACCD;"> id</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">String</span><span style="color:#A6ACCD;"> name</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">String</span><span style="color:#A6ACCD;"> country</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_3、创建mapper-1" tabindex="-1">3、创建Mapper <a class="header-anchor" href="#_3、创建mapper-1" aria-label="Permalink to &quot;3、创建Mapper&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Mapper</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">interface</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">authorMapper</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">extends</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">BaseMapper</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">author</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="_4、配置数据源" tabindex="-1">4、配置数据源 <a class="header-anchor" href="#_4、配置数据源" aria-label="Permalink to &quot;4、配置数据源&quot;">​</a></h3><p>创建application-dev.yml</p><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">spring</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">datasource</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">driver-class-name</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">com.mysql.cj.jdbc.Driver</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">url</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">jdbc:mysql://192.168.22.145:3321/readwrite_splitting_db?serverTimezone=GMT%2B8&amp;useSSL=false</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">password</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">username</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root</span></span>
<span class="line"><span style="color:#F07178;">mybatis-plus</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">configuration</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">log-impl</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">org.apache.ibatis.logging.stdout.StdOutImpl</span></span></code></pre></div><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">spring.profiles.active</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">dev</span></span></code></pre></div><h3 id="_5、测试" tabindex="-1">5、测试 <a class="header-anchor" href="#_5、测试" aria-label="Permalink to &quot;5、测试&quot;">​</a></h3><div class="language-java"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Resource</span></span>
<span class="line"><span style="color:#C792EA;">private</span><span style="color:#A6ACCD;"> authorMapper authorMapper</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">// 读数据测试</span></span>
<span class="line"><span style="color:#89DDFF;">@</span><span style="color:#C792EA;">Test</span></span>
<span class="line"><span style="color:#C792EA;">public</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">void</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">testSelect</span><span style="color:#89DDFF;">()</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C792EA;">List</span><span style="color:#89DDFF;">&lt;</span><span style="color:#C792EA;">author</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> authors </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> authorMapper</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">selectList</span><span style="color:#89DDFF;">(null);</span></span>
<span class="line"><span style="color:#A6ACCD;">    authors</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">forEach</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">System</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">out</span><span style="color:#89DDFF;font-style:italic;">::</span><span style="color:#A6ACCD;">println</span><span style="color:#89DDFF;">);</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172320977.png" alt="image-20220917232025903" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172316705.png" alt="image-20220917231613610" style="zoom:80%;"><h1 id="shardingsphere-proxy垂直分片" tabindex="-1">ShardingSphere-Proxy垂直分片 <a class="header-anchor" href="#shardingsphere-proxy垂直分片" aria-label="Permalink to &quot;ShardingSphere-Proxy垂直分片&quot;">​</a></h1><h2 id="配置演示-1" tabindex="-1">配置演示 <a class="header-anchor" href="#配置演示-1" aria-label="Permalink to &quot;配置演示&quot;">​</a></h2><h3 id="_1、修改配置config-sharding-yaml" tabindex="-1">1、修改配置config-sharding.yaml <a class="header-anchor" href="#_1、修改配置config-sharding-yaml" aria-label="Permalink to &quot;1、修改配置config-sharding.yaml&quot;">​</a></h3><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">schemaName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sharding_db1</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F07178;">dataSources</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">ds_0</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">url</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">jdbc:mysql://192.168.22.145:3306/db_user?serverTimezone=UTC&amp;useSSL=false</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">username</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">password</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">123456</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">connectionTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">idleTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxLifetimeMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1800000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">50</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">minPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">ds_1</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">url</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">jdbc:mysql://192.168.0.155:3307/db_order?serverTimezone=UTC&amp;useSSL=false</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">username</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">password</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">123456</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">connectionTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">idleTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxLifetimeMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1800000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">50</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">minPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F07178;">rules</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">!SHARDING</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">tables</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">t_user</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">actualDataNodes</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">ds_0.t_user</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">t_order</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">actualDataNodes</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">ds_1.t_order</span></span></code></pre></div><h3 id="_2、重启服务-1" tabindex="-1">2、重启服务 <a class="header-anchor" href="#_2、重启服务-1" aria-label="Permalink to &quot;2、重启服务&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">bin/stop.sh</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3321</span></span>
<span class="line"><span style="color:#FFCB6B;">bin/start.sh</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3321</span></span></code></pre></div><h3 id="_3、访问测试-1" tabindex="-1">3、访问测试 <a class="header-anchor" href="#_3、访问测试-1" aria-label="Permalink to &quot;3、访问测试&quot;">​</a></h3><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mysql </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">h127.0.</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">P3321 </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">uroot </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p</span></span>
<span class="line"><span style="color:#A6ACCD;">show databases;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181306715.png" alt="image-20220918130650589" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> show databases;</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> sharding_db1;</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> show tables;</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> t_order;</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> t_user;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209172304681.png" alt="image-20220917230432536" style="zoom:80%;"><h3 id="_4、实时查看日志-1" tabindex="-1">4、实时查看日志 <a class="header-anchor" href="#_4、实时查看日志-1" aria-label="Permalink to &quot;4、实时查看日志&quot;">​</a></h3><p>可以通过这种方式查看服务器中输出的SQL语句</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">tail</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/shardingsphere-proxy/logs/stdout.log</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181309157.png" alt="image-20220918130900986" style="zoom:80%;"><h1 id="shardingsphere-proxy水平分片" tabindex="-1">ShardingSphere-Proxy水平分片 <a class="header-anchor" href="#shardingsphere-proxy水平分片" aria-label="Permalink to &quot;ShardingSphere-Proxy水平分片&quot;">​</a></h1><h2 id="_1、修改配置文件-1" tabindex="-1">1、修改配置文件 <a class="header-anchor" href="#_1、修改配置文件-1" aria-label="Permalink to &quot;1、修改配置文件&quot;">​</a></h2><p><strong>修改配置config-sharding.yaml</strong></p><div class="language-yaml"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F07178;">schemaName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sharding_db2</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F07178;">dataSources</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">ds_order0</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">url</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">jdbc:mysql://192.168.22.145:3306/db_order?serverTimezone=UTC&amp;useSSL=false</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">username</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">password</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">123456</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">connectionTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">idleTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxLifetimeMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1800000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">50</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">minPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">ds_order1</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">url</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">jdbc:mysql://192.168.0.155:3307/db_order?serverTimezone=UTC&amp;useSSL=false</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">username</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">root</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">password</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">123456</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">connectionTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">idleTimeoutMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxLifetimeMilliseconds</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1800000</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">maxPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">50</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">minPoolSize</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F07178;">rules</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#C792EA;">!SHARDING</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">tables</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">t_order</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">actualDataNodes</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">ds_order${0..1}.t_order${0..1}</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">databaseStrategy</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">standard</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">          </span><span style="color:#F07178;">shardingColumn</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">user_id</span></span>
<span class="line"><span style="color:#A6ACCD;">          </span><span style="color:#F07178;">shardingAlgorithmName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">alg_mod</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">tableStrategy</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">standard</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">          </span><span style="color:#F07178;">shardingColumn</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">order_no</span></span>
<span class="line"><span style="color:#A6ACCD;">          </span><span style="color:#F07178;">shardingAlgorithmName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">alg_hash_mod</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">keyGenerateStrategy</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">column</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">id</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">keyGeneratorName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">snowflake</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">t_order_item</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">actualDataNodes</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">ds_order${0..1}.t_order_item${0..1}</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">databaseStrategy</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">standard</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">          </span><span style="color:#F07178;">shardingColumn</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">user_id</span></span>
<span class="line"><span style="color:#A6ACCD;">          </span><span style="color:#F07178;">shardingAlgorithmName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">alg_mod</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">tableStrategy</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">standard</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">          </span><span style="color:#F07178;">shardingColumn</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">order_no</span></span>
<span class="line"><span style="color:#A6ACCD;">          </span><span style="color:#F07178;">shardingAlgorithmName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">alg_hash_mod</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">keyGenerateStrategy</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">column</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">id</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">keyGeneratorName</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">snowflake</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">bindingTables</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">t_order,t_order_item</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">broadcastTables</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">t_dict</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">shardingAlgorithms</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">alg_inline_userid</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">type</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">INLINE</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">props</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">algorithm-expression</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">server-order$-&gt;{user_id % 2}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">alg_mod</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">type</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">MOD</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">props</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">sharding-count</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">alg_hash_mod</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">type</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">HASH_MOD</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">props</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F07178;">sharding-count</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#F07178;">keyGenerators</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#F07178;">snowflake</span><span style="color:#89DDFF;">:</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#F07178;">type</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">SNOWFLAKE</span></span></code></pre></div><h2 id="_2、重启服务-2" tabindex="-1">2、重启服务 <a class="header-anchor" href="#_2、重启服务-2" aria-label="Permalink to &quot;2、重启服务&quot;">​</a></h2><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">bin/stop.sh</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3321</span></span>
<span class="line"><span style="color:#FFCB6B;">bin/start.sh</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3321</span></span></code></pre></div><h2 id="_3、访问测试-2" tabindex="-1">3、访问测试 <a class="header-anchor" href="#_3、访问测试-2" aria-label="Permalink to &quot;3、访问测试&quot;">​</a></h2><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">-- 没进去就再启动，要么就等一会</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">h127.0.</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">.</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">P3321 </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">uroot </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p</span></span>
<span class="line"><span style="color:#A6ACCD;">show databases;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181314915.png" alt="image-20220918131452786" style="zoom:80%;"><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> show databases;</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">use</span><span style="color:#A6ACCD;"> sharding_db2;</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> show tables;</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> t_order; </span><span style="color:#676E95;font-style:italic;">--测试水平分片</span></span>
<span class="line"><span style="color:#A6ACCD;">mysql</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">select</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">from</span><span style="color:#A6ACCD;"> t_dict; </span><span style="color:#676E95;font-style:italic;">--测试广播表</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181315312.png" alt="image-20220918131542185" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181315099.png" alt="image-20220918131556896" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181316345.png" alt="image-20220918131606207" style="zoom:80%;"><h2 id="_4、实时查看日志-2" tabindex="-1">4、实时查看日志 <a class="header-anchor" href="#_4、实时查看日志-2" aria-label="Permalink to &quot;4、实时查看日志&quot;">​</a></h2><p>可以通过这种方式查看服务器中输出的SQL语句</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">tail</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/shardingsphere-proxy/logs/stdout.log</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202209181316358.png" alt="image-20220918131622107" style="zoom:80%;"></div></div></main><footer class="VPDocFooter" data-v-6b87e69f data-v-37656e44><!--[--><!--]--><!----><nav class="prev-next" data-v-37656e44><div class="pager" data-v-37656e44><a class="pager-link prev" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%90%E7%BB%B4.html" data-v-37656e44><span class="desc" data-v-37656e44>Previous page</span><span class="title" data-v-37656e44>MySQL运维</span></a></div><div class="pager" data-v-37656e44><a class="pager-link next" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%9F%BA%E7%A1%80.html" data-v-37656e44><span class="desc" data-v-37656e44>Next page</span><span class="title" data-v-37656e44>Redis基础</span></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    <script>window.__VP_HASH_MAP__=JSON.parse("{\"2、数据库_mysql_mysql面试_基础.md\":\"40da680a\",\"1、学前端_5、小程序_小程序项目.md\":\"60a1629b\",\"1、学前端_4、node_知识篇.md\":\"a7fb500e\",\"1、学前端_2、js_ts_es6 进阶.md\":\"6d07ba10\",\"1、学前端_3、vue_vue3_vue3进阶.md\":\"7ac622b4\",\"5、运维_jenkins.md\":\"929081f8\",\"1、学前端_2、js_ts_typescript.md\":\"875a4aa4\",\"2、数据库_mysql_mysql核心_设计.md\":\"7faf46d1\",\"2、数据库_mysql_mysql核心_基础.md\":\"d8e97f3e\",\"1、学前端_1、html_css_html基础.md\":\"7584d076\",\"1、学前端_5、专题篇_问题篇.md\":\"e893aaa2\",\"2、数据库_mysql_mysql面试_进阶.md\":\"f934806d\",\"3、springboot_运维_原理.md\":\"f4a39db6\",\"2、数据库_influxdb.md\":\"6e1711e1\",\"3、springboot_新特性.md\":\"cdf3e307\",\"mybatis_mybatisplus_jpa.md\":\"8e41681b\",\"1、学前端_5、小程序_小程序优化.md\":\"a2185198\",\"2、数据库_redis_redis基础.md\":\"856df0e0\",\"linux_实用脚本.md\":\"f2299dd5\",\"4、微服务_必备_分布式基础.md\":\"d49863d5\",\"2、数据库_redis_redis优化.md\":\"e66ae32f\",\"4、微服务_springsecurity_进阶篇.md\":\"235a8e9e\",\"5、运维_chatgpt.md\":\"10db3823\",\"2、数据库_mysql_分库分表.md\":\"e1c8a095\",\"start.md\":\"9bc1ff8d\",\"5、运维_github.md\":\"2ec6c735\",\"java学前端_css.md\":\"f11b47f0\",\"1、学前端_5、专题篇_知识篇.md\":\"a463ed8d\",\"linux_软件部署.md\":\"d6722925\",\"2、数据库_neo4j.md\":\"97ad22ac\",\"team.md\":\"ce467a6a\",\"nginx_实战篇.md\":\"7785486e\",\"index.md\":\"8c3ec167\",\"计算机基础_计算机网络_网络基础.md\":\"7a54a85d\",\"1、学前端_4、node_进阶篇.md\":\"60f6db69\",\"java_java集合.md\":\"a049b313\",\"1、学前端_3、vue_vue3_vue3高级.md\":\"614d1516\",\"1、学前端_5、小程序_微信小程序.md\":\"9a4be771\",\"5、运维_netty.md\":\"12ca0278\",\"2、数据库_mysql_mysql核心_运维.md\":\"83f97c16\",\"idea_vs code.md\":\"afdcb593\",\"java学前端_vue3_组件.md\":\"1086884e\",\"idea_chrome.md\":\"4a32afbc\",\"云原生_k8s.md\":\"db58e65a\",\"2、数据库_mysql_mysql核心_进阶.md\":\"61d16dff\",\"ssm_springbatch.md\":\"f799ab4a\",\"三高_分布式.md\":\"db1b8a1b\",\"2、数据库_elasticsearch_1、es基础.md\":\"04d17448\",\"linux_linux基础.md\":\"4b0bf394\",\"idea_idea插件.md\":\"fa86e45a\",\"可视化 _ 监控_可视化大屏.md\":\"004553bd\",\"2、数据库_mongodb_整合.md\":\"3c47d7f4\",\"4、微服务_springsecurity_基础篇.md\":\"534a3401\",\"4、微服务_进阶.md\":\"69095c58\",\"计算机基础_计算机基础_操作系统.md\":\"0f75d113\",\"可视化 _ 监控_zabbix.md\":\"71f2270e\",\"nginx_基础篇.md\":\"c7d8bb50\",\"1、学前端_4、node_项目实战.md\":\"bc5065b8\",\"2、数据库_redis_redis原理.md\":\"5cedf685\",\"可视化 _ 监控_监控基础.md\":\"ac56ce4d\",\"三高_高并发.md\":\"ea9ffc99\",\"2、数据库_redis_redis高级.md\":\"1d5872f6\",\"1、学前端_4、node_基础篇.md\":\"581cc13a\",\"2、数据库_mongodb_基础.md\":\"fb7a0a29\",\"idea_idea基础.md\":\"6f2f9638\",\"4、微服务_必备_sentinel.md\":\"2edfbf6c\",\"2、数据库_elasticsearch_3、es高级.md\":\"ef146606\",\"1、学前端_3、vue_vue3_vue3新语法.md\":\"8afd5409\",\"消息中间件_canal.md\":\"3949163c\",\"ssm_maven.md\":\"2c5e12ed\",\"4、微服务_springsecurity_高级篇.md\":\"882d3ff3\",\"linux_linux进阶.md\":\"188ef7b4\",\"计算机基础_设计模式_uml.md\":\"634ba256\",\"计算机基础_算法_leetcode.md\":\"77162fb9\",\"项目实战_小兔鲜_进阶篇1.md\":\"17c52c81\",\"1、学前端_2、js_ts_es6 基础.md\":\"fda3f18b\",\"项目实战_小兔鲜_进阶篇2.md\":\"a0f23006\",\"软件测试_测试基础.md\":\"8c1060cd\",\"2、数据库_redis_本地缓存.md\":\"00617fe6\",\"nginx_面试篇.md\":\"e3fb373a\",\"mybatis_mybatisplus_mybatis.md\":\"9239e0ad\",\"linux_shell.md\":\"ae53d83b\",\"2、数据库_mysql_mysql核心_优化.md\":\"36230425\",\"项目实战_项目推荐.md\":\"f9d97630\",\"mybatis_mybatisplus_mybatisplus.md\":\"0030fd35\",\"项目实战_百度地图_进阶篇.md\":\"c8b93267\",\"三高_高可用.md\":\"323840c5\",\"java_java新特性.md\":\"22abf56d\",\"软件测试_压力测试.md\":\"9ab44440\",\"java学前端_html_js.md\":\"e0fcd240\",\"2、数据库_redis_redis实战.md\":\"d6daeeab\",\"nginx_进阶篇.md\":\"e6b63195\",\"三高_秒杀.md\":\"3878bb64\",\"5、运维_git.md\":\"0264925c\",\"java_java进阶.md\":\"e79cb5b4\",\"并发 _ 多线程_基础篇.md\":\"7adbfac5\",\"项目实战_百度地图_基础篇.md\":\"8afa5954\",\"java学前端_react.md\":\"3ec827dd\",\"1、学前端_1、html_css_css基础.md\":\"01b56712\",\"项目实战_小兔鲜_基础篇.md\":\"646f5df5\",\"1、学前端_2、js_ts_js 基础.md\":\"cb13e36f\",\"可视化 _ 监控_监控进阶.md\":\"0cdbc292\",\"计算机基础_设计模式_基础篇.md\":\"51617287\",\"计算机基础_数据结构_基础篇.md\":\"b2bfd8d4\",\"项目实战_苍穹外卖_进阶篇.md\":\"48415e41\",\"ssm_spring.md\":\"ab514659\",\"消息中间件_rabbitmq.md\":\"45b1eb28\",\"1、学前端_1、html_css_网页进阶.md\":\"db998248\",\"消息中间件_kafka.md\":\"b747dabf\",\"云原生_docker.md\":\"983c7ba7\",\"4、微服务_必备_分布式锁.md\":\"5af1cf8d\",\"消息中间件_rocketmq.md\":\"d441da85\",\"项目实战_黑马头条_基础篇.md\":\"b05af3a6\",\"ssm_springmvc.md\":\"81b9714f\",\"项目实战_支付.md\":\"1d7407dd\",\"项目实战_黑马头条_进阶篇2.md\":\"bff0015b\",\"项目实战_黑马头条_进阶篇.md\":\"19f18388\",\"java学前端_vue2_组件.md\":\"58c6b1df\",\"3、springboot_基础篇.md\":\"529c66f4\",\"3、springboot_应用篇.md\":\"8b92aa61\",\"项目实战_黑马头条_高级篇.md\":\"227c08c1\",\"1、学前端_5、小程序_uniapp.md\":\"71a282b4\",\"项目实战_云尚办公_基础篇.md\":\"1fe188ba\",\"并发 _ 多线程_并发完善.md\":\"26619c46\",\"1、学前端_2、js_ts_js 进阶.md\":\"657dfb8f\",\"java_java高级.md\":\"23782d1a\",\"java_java基础.md\":\"86d67c77\"}");window.__VP_SITE_DATA__=JSON.parse("{\"lang\":\"en-US\",\"dir\":\"ltr\",\"title\":\"VitePress\",\"description\":\"A VitePress site\",\"base\":\"/notebook/\",\"head\":[],\"appearance\":true,\"themeConfig\":{\"algolia\":{\"appId\":\"DW7O63I9IR\",\"apiKey\":\"f8ed758cdb288a8b06542bc35923c1a1\",\"indexName\":\"notebook\"},\"sidebar\":[{\"text\":\"Java\",\"collapsed\":true,\"items\":[{\"text\":\"Java基础\",\"link\":\"/Java/Java基础\"},{\"text\":\"Java新特性\",\"link\":\"/Java/Java新特性\"},{\"text\":\"Java进阶\",\"link\":\"/Java/Java进阶\"},{\"text\":\"Java集合\",\"link\":\"/Java/Java集合\"},{\"text\":\"Java高级\",\"link\":\"/Java/Java高级\"}]},{\"text\":\"Linux\",\"collapsed\":true,\"items\":[{\"text\":\"Linux基础\",\"link\":\"/Linux/Linux基础\"},{\"text\":\"Linux新特性\",\"link\":\"/Linux/Linux进阶\"},{\"text\":\"Shell脚本\",\"link\":\"/Linux/Shell\"},{\"text\":\"实用脚本\",\"link\":\"/Linux/实用脚本\"},{\"text\":\"软件部署\",\"link\":\"/Linux/软件部署\"}]},{\"text\":\"Nginx\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/Nginx/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/Nginx/进阶篇\"},{\"text\":\"实战篇\",\"link\":\"/Nginx/实战篇\"},{\"text\":\"面试篇\",\"link\":\"/Nginx/面试篇\"}]},{\"text\":\"SSM\",\"collapsed\":true,\"items\":[{\"text\":\"Maven\",\"link\":\"/SSM/Maven\"},{\"text\":\"Spring\",\"link\":\"/SSM/Spring\"},{\"text\":\"SpringMVC\",\"link\":\"/SSM/SpringMVC\"},{\"text\":\"SpringBatch\",\"link\":\"/SSM/SpringBatch\"}]},{\"text\":\"SpringBoot\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/3、SpringBoot/基础篇\"},{\"text\":\"应用篇\",\"link\":\"/3、SpringBoot/应用篇\"},{\"text\":\"新特性\",\"link\":\"/3、SpringBoot/新特性\"},{\"text\":\"运维&原理\",\"link\":\"/3、SpringBoot/运维&原理\"}]},{\"text\":\"SpringCloud\",\"collapsed\":true,\"items\":[{\"text\":\"SpringCloud\",\"link\":\"/4、微服务/进阶\"},{\"text\":\"Sentinel\",\"link\":\"/4、微服务/必备/Sentinel\"}]},{\"text\":\"SpringSecurity\",\"collapsed\":true,\"items\":[{\"text\":\"SpringSecurity基础篇\",\"link\":\"/4、微服务/SpringSecurity/基础篇\"},{\"text\":\"SpringSecurity进阶篇\",\"link\":\"/4、微服务/SpringSecurity/进阶篇\"},{\"text\":\"SpringSecurity高级篇\",\"link\":\"/4、微服务/SpringSecurity/高级篇\"}]},{\"text\":\"Mybatis & MybatisPlus\",\"collapsed\":true,\"items\":[{\"text\":\"Mybatis\",\"link\":\"/Mybatis&MybatisPlus/Mybatis\"},{\"text\":\"MybatisPlus\",\"link\":\"/Mybatis&MybatisPlus/MybatisPlus\"},{\"text\":\"JPA\",\"link\":\"/Mybatis&MybatisPlus/JPA\"}]},{\"text\":\"Git & ChatGPT\",\"collapsed\":true,\"items\":[{\"text\":\"Git\",\"link\":\"/5、运维/Git\"},{\"text\":\"Github\",\"link\":\"/5、运维/Github\"},{\"text\":\"ChatGPT\",\"link\":\"/5、运维/ChatGPT\"},{\"text\":\"Jenkins\",\"link\":\"/5、运维/Jenkins\"},{\"text\":\"Netty\",\"link\":\"/5、运维/Netty\"}]},{\"text\":\"数据库\",\"collapsed\":true,\"items\":[{\"text\":\"MySQL\",\"collapsed\":true,\"items\":[{\"text\":\"MySQL基础\",\"link\":\"/2、数据库/MySQL/MySQL核心/基础\"},{\"text\":\"MySQL进阶\",\"link\":\"/2、数据库/MySQL/MySQL核心/进阶\"},{\"text\":\"MySQL优化\",\"link\":\"/2、数据库/MySQL/MySQL核心/优化\"},{\"text\":\"MySQL设计\",\"link\":\"/2、数据库/MySQL/MySQL核心/设计\"},{\"text\":\"MySQL运维\",\"link\":\"/2、数据库/MySQL/MySQL核心/运维\"},{\"text\":\"分库分表\",\"link\":\"/2、数据库/MySQL/分库分表\"}]},{\"text\":\"Redis\",\"collapsed\":true,\"items\":[{\"text\":\"Redis基础\",\"link\":\"/2、数据库/Redis/Redis基础\"},{\"text\":\"Redis优化\",\"link\":\"/2、数据库/Redis/Redis优化\"},{\"text\":\"Redis原理\",\"link\":\"/2、数据库/Redis/Redis原理\"},{\"text\":\"Redis高级\",\"link\":\"/2、数据库/Redis/Redis高级\"},{\"text\":\"Redis实战\",\"link\":\"/2、数据库/Redis/Redis实战\"},{\"text\":\"本地缓存\",\"link\":\"/2、数据库/Redis/本地缓存\"}]},{\"text\":\"MongoDB\",\"collapsed\":true,\"items\":[{\"text\":\"MongoDB基础\",\"link\":\"/2、数据库/MongoDB/基础\"},{\"text\":\"MongoDB进阶\",\"link\":\"/2、数据库/MongoDB/整合\"}]},{\"text\":\"ElasticSearch\",\"collapsed\":true,\"items\":[{\"text\":\"ES基础\",\"link\":\"/2、数据库/ElasticSearch/1、ES基础\"},{\"text\":\"ES高级\",\"link\":\"/2、数据库/ElasticSearch/3、ES高级\"}]},{\"text\":\"InfluxDB\",\"link\":\"/2、数据库/influxdb\"},{\"text\":\"Neo4j\",\"link\":\"/2、数据库/Neo4j\"}]},{\"text\":\"高并发 & 秒杀 & 分布式\",\"collapsed\":true,\"items\":[{\"text\":\"分布式理论\",\"link\":\"/三高/分布式\"},{\"text\":\"分布式锁\",\"link\":\"/4、微服务/必备/分布式锁\"},{\"text\":\"秒杀\",\"link\":\"/三高/秒杀\"},{\"text\":\"高可用\",\"link\":\"/三高/高可用\"},{\"text\":\"高并发\",\"link\":\"/三高/高并发\"}]},{\"text\":\"云原生\",\"collapsed\":true,\"items\":[{\"text\":\"Docker\",\"link\":\"/云原生/Docker\"},{\"text\":\"K8S\",\"link\":\"/云原生/K8S\"}]},{\"text\":\"可视化 & 监控\",\"collapsed\":true,\"items\":[{\"text\":\"监控基础\",\"link\":\"/可视化 & 监控/监控基础\"},{\"text\":\"监控进阶\",\"link\":\"/可视化 & 监控/监控进阶\"},{\"text\":\"可视化大屏\",\"link\":\"/可视化 & 监控/可视化大屏\"},{\"text\":\"Zabbix\",\"link\":\"/可视化 & 监控/Zabbix\"}]},{\"text\":\"学前端\",\"collapsed\":true,\"items\":[{\"text\":\"HTML+CSS\",\"collapsed\":true,\"items\":[{\"text\":\"HTML基础\",\"link\":\"/1、学前端/1、HTML+CSS/HTML基础\"},{\"text\":\"CSS基础\",\"link\":\"/1、学前端/1、HTML+CSS/CSS基础\"},{\"text\":\"网页进阶\",\"link\":\"/1、学前端/1、HTML+CSS/网页进阶\"}]},{\"text\":\"JS+TS\",\"collapsed\":true,\"items\":[{\"text\":\"JS基础\",\"link\":\"/1、学前端/2、JS+TS/JS 基础\"},{\"text\":\"JS进阶\",\"link\":\"/1、学前端/2、JS+TS/JS 进阶\"},{\"text\":\"ES6基础\",\"link\":\"/1、学前端/2、JS+TS/ES6 基础\"},{\"text\":\"ES6进阶\",\"link\":\"/1、学前端/2、JS+TS/ES6 进阶\"},{\"text\":\"TS基础\",\"link\":\"/1、学前端/2、JS+TS/TypeScript\"}]},{\"text\":\"NodeJS\",\"collapsed\":true,\"items\":[{\"text\":\"Node基础\",\"link\":\"/1、学前端/4、Node/基础篇\"},{\"text\":\"Node进阶\",\"link\":\"/1、学前端/4、Node/进阶篇\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/4、Node/项目实战\"}]},{\"text\":\"Vue\",\"collapsed\":true,\"items\":[{\"text\":\"Vue3进阶\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3进阶\"},{\"text\":\"Vue3高级\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3高级\"},{\"text\":\"Vue3新语法\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3新语法\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/3、Vue/Vue2/Vue2项目\"}]},{\"text\":\"小程序\",\"collapsed\":true,\"items\":[{\"text\":\"小程序基础\",\"link\":\"/1、学前端/5、小程序/微信小程序\"},{\"text\":\"小程序优化\",\"link\":\"/1、学前端/5、小程序/小程序优化\"},{\"text\":\"uniapp\",\"link\":\"/1、学前端/5、小程序/uniapp\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/5、小程序/小程序项目\"}]}]},{\"text\":\"计算机基础\",\"collapsed\":true,\"items\":[{\"text\":\"数据结构\",\"link\":\"/计算机基础/数据结构/基础篇\"},{\"text\":\"操作系统\",\"link\":\"/计算机基础/计算机基础/操作系统\"},{\"text\":\"设计模式\",\"link\":\"/计算机基础/设计模式/基础篇\"},{\"text\":\"计算机网络\",\"link\":\"/计算机基础/计算机网络/网络基础\"},{\"text\":\"UML\",\"link\":\"/计算机基础/设计模式/UML\"},{\"text\":\"LeetCode\",\"link\":\"/计算机基础/算法/LeetCode\"}]},{\"text\":\"项目实战\",\"collapsed\":true,\"items\":[{\"text\":\"云尚办公\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/云尚办公/基础篇\"}]},{\"text\":\"小兔鲜\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/小兔鲜/基础篇\"},{\"text\":\"进阶篇1\",\"link\":\"/项目实战/小兔鲜/进阶篇1\"},{\"text\":\"进阶篇2\",\"link\":\"/项目实战/小兔鲜/进阶篇2\"}]},{\"text\":\"地图\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/百度地图/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/项目实战/百度地图/进阶篇\"}]},{\"text\":\"苍穹外卖\",\"collapsed\":true,\"items\":[{\"text\":\"进阶篇\",\"link\":\"/项目实战/苍穹外卖/进阶篇\"}]},{\"text\":\"黑马头条\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/黑马头条/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/项目实战/黑马头条/进阶篇\"},{\"text\":\"进阶篇2\",\"link\":\"/项目实战/黑马头条/进阶篇2\"},{\"text\":\"高级篇\",\"link\":\"/项目实战/黑马头条/高级篇\"}]},{\"text\":\"支付\",\"link\":\"/项目实战/支付\"},{\"text\":\"项目推荐\",\"link\":\"/项目实战/项目推荐\"}]},{\"text\":\"团队成员\",\"link\":\"/team\"}],\"siteTitle\":\"任硕的文档\",\"logo\":\"/Vue.png\",\"nav\":[{\"text\":\"Java学前端\",\"items\":[{\"items\":[{\"text\":\"HTML+JS\",\"link\":\"/Java学前端/HTML+JS\"},{\"text\":\"CSS\",\"link\":\"/Java学前端/CSS\"},{\"text\":\"Vue2+组件\",\"link\":\"/Java学前端/Vue2+组件\"},{\"text\":\"Vue3+组件\",\"link\":\"/Java学前端/Vue3+组件\"},{\"text\":\"React\",\"link\":\"/Java学前端/React\"}]}],\"activeMatch\":\"/Java/\"},{\"text\":\"软件测试\",\"items\":[{\"items\":[{\"text\":\"测试基础\",\"link\":\"/软件测试/测试基础\"},{\"text\":\"压力测试\",\"link\":\"/软件测试/压力测试\"}]}]},{\"text\":\"多线程\",\"items\":[{\"items\":[{\"text\":\"基础篇\",\"link\":\"/并发 & 多线程/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/并发 & 多线程/并发完善\"}]}]},{\"text\":\"开发工具\",\"items\":[{\"items\":[{\"text\":\"Chrome\",\"link\":\"/IDEA/Chrome\"},{\"text\":\"IDEA基础\",\"link\":\"/IDEA/IDEA基础\"},{\"text\":\"IDEA插件\",\"link\":\"/IDEA/IDEA插件\"},{\"text\":\"VS Code\",\"link\":\"/IDEA/VS Code\"}]}]},{\"text\":\"消息中间件\",\"items\":[{\"items\":[{\"text\":\"RabbitMQ\",\"link\":\"/消息中间件/RabbitMQ\"},{\"text\":\"RocketMQ\",\"link\":\"/消息中间件/RocketMQ\"},{\"text\":\"Kafka\",\"link\":\"/消息中间件/Kafka\"},{\"text\":\"Canal\",\"link\":\"/消息中间件/Canal\"}]}]}],\"socialLinks\":[{\"icon\":\"github\",\"link\":\"https://github.com/renshuo123/renshuo123.github.io\"},{\"icon\":\"twitter\",\"link\":\"#\"},{\"icon\":{\"svg\":\"<svg t=\\\"1676028692954\\\" class=\\\"icon\\\" ...</path></svg>\"},\"link\":\"https://github.com/\"}]},\"locales\":{},\"scrollOffset\":90,\"cleanUrls\":false}");</script>
    
  </body>
</html>